Using AngularJS to apply a filter on the parent element only if the same filter has been applied on its

I have a list of items that can be filtered based on a value entered in a text box. I am trying to create a new filtered list that includes the parent and its children only if the parent has at least one filtered item. If the parent has no filtered items, it should not be displayed.

For example: If I enter "3D" in the text box, I want to exclude "Day parts" and "Week parts" from the list as they no longer have any children after filtering.

<div ng-controller="Ctrl">
 <input type="text" data-ng-model="inputValue.name"/>
<ul data-ng-repeat="condition in conditions">

        <div data-ng-click="setHeadingStatus(condition)">
            <div>
                {{condition.name}}
            </div>
        <li ng-repeat="item in condition.items | filter:inputValue">

                    <div class="custom-typeahead-list-title">{{item.name}}</div>
                    <div class="custom-typeahead-list-desc">{{item.description}}</div>

        </li>
            </div>
</ul>

function Ctrl($scope , $filter){

$scope.countryCode = 1;

$scope.conditions = [
{
    "id": 1,
    "name": "Experiences",
    "expanded": false,
    "items": [
        {
            "id": 1,
            "name": "3D"

        },
        {
            "id": 2,
            "name": "Imax"
        },
        {
            "id": 3,
            "name": "D-Box"
        },
        {
            "id": 4,
            "name": "THX"
        }
    ]
},
{
    "id": 2,
    "name": "Day parts",
    "expanded": false,
    "items": [
        {
            "id": 1,
            "name": "Early Bird"
        },
        {
            "id": 2,
            "name": "Matinee"
        },
        {
            "id": 3,
            "name": "Last Night"
        }
    ]
},
{
    "id": 3,
    "name": "Week parts",
    "expanded": false,
    "items": [
        {
            "id": 1,
            "name": "Monday"
        },
        {
            "id": 2,
            "name": "Wednesday"
        },
        {
            "id": 3,
            "name": "Weekend"
        }
    ]
} 
  ]

}

Here is the example of it
http://jsfiddle.net/KJ3Nx/48/

Answer №1

To show each block, simply add an ng-show directive with the filtered length as the condition:

<div data-ng-show="(filteredItems = (items | filter:searchValue)).length > 0" data-ng-click="updateStatus(filteredItems)">

Check out the updated fiddle here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing HTML "range" element with mouse scroll functionality for incrementing values in step increments

I'm working on developing a scroll feature that operates independently from the main window's scrolling function. I aim to trigger specific events in the primary window based on interactions with this separate scrollbar. The only solution I coul ...

Issue with AngularJS Plunker functionality

I'm baffled as to why it isn't functioning properly. Here's the Plnkr link. Interestingly, the same file is working perfectly fine on this Plnkr link. ...

Arrays crossing in the world of meteors

Currently, I am attempting to extract an array from a different collection using collection2. So far, I have managed to accomplish this with objects, as demonstrated in the example below for users: users: { type: String, label: "Inspector", option ...

Exploring the power of Node.js and EJS through the art of

Recently delving into the world of node.js, I encountered a puzzling issue with my EJS template. It seems that my for loop is not executing properly within the EJS template while attempting to create a basic todo app. Below is the structure of the project ...

How can I incorporate the Three.js scene demonstration into Vue 3?

Looking to integrate the Three.js beginner example for creating a scene [1] into a Vue.js 3 project using JavaScript (not TypeScript)? [1] main.js import * as THREE from 'three'; const scene = new THREE.Scene(); const camera = new THREE.Perspe ...

Error encountered in Vue code, lacks default export on Editor Terminal

I'm not experiencing any issues in the browser, I am getting the desired output. However, why does this keep showing up in the editor terminal? Any assistance would be greatly appreciated. Error - No Default Export: The module "/vue3/src/components/ ...

Creating a sticky popup feature for your Chrome extension

Just starting out with chrome extensions and looking to create one that appends an external class to a selected tag. For example, let's say we have the following tag: <h1>extension</h1> When the user clicks on this element, I want to ad ...

Using Flask to pass variable data from one route to another in Python using the `url

I am facing an issue with sending a variable value to Python from Flask HTML/JS via url_for(). Here's my Python code: @app.route('/video_feed/<device>') def video_feed(device): # return the response generated along with the speci ...

What is the best way to establish a default selection in Angular?

After retrieving JSON data from the server, it looks something like this: $scope.StateList = {"States": [ { "Id": 1, "Code": "AL", "Name": "Alabama" }, { "Id": 2, "Code": "AK", "Name": "Alask ...

The issue of button onclick textfield validation malfunctioning

Within this container, there is a text field identified as 'codeCityId' along with a button that triggers an onclick method. When the button is clicked, the function will verify the input. function click()={ var valid = $('#codeCityId' ...

Node.js Sequelize QueryExplore the power of Sequelize in Node.js

I'm looking to filter the "incomers" based on age, but all I have in the table is their date of birth. I want to find people within a specific age range, how can I accomplish this? router.post('/', function (req, res, next) { let parame ...

What is the best way to store the retrieved information from an AngularJS $http request in a cache?

My AngularJS Javascript code interacts with a web service to retrieve a simple array of objects: this.$http({url: '/api/Action/GetActions', method: "GET" }) .success((data) => { self.Actions = data; }) I am exploring options f ...

Issue with Angular app occurring on one device, functioning properly on others - Error: $injector:unpr Unknown Provider

Whenever I try to run my application on my usual Windows machine, I keep encountering the following error: Error: $injector:unpr Unknown Provider Interestingly, the app works perfectly fine on other machines running Windows, MAC, and Linux. Since the we ...

What is the best approach to have a React page render only the specific component that has changed, rather than re

Recently, I've been facing an issue with a toggle implementation where the page always re-renders the entire content upon the first click of the toggle. Here is a snippet of how it looks: export default function Toggle({isChecked, label}: Props) { r ...

Instead of conducting validation on the returned value in the service, it is recommended to validate it

I currently have a method in my service as shown below. module.service('Service', function($state, $rootScope) { var getRemoteItems = function(page, displayLimit) { var defferer = $q.defer() var query = new Parse.Query("It ...

What is the best way to send an array to the server with $http in AngularJS?

I have a collection of unique identifiers that I need to send to the server. Most online solutions suggest passing these identifiers as query parameters in the URL, but this approach is not ideal for me as there could be a large number of identifiers invol ...

Struggling to display the preloader animation while waiting for the render.com server to start up (using the free tier web service)

My choice for deploying dynamic websites is render.com and I am currently using their free tier. The issue with this free service is that Render spins down the web service after 15 minutes of inactivity, resulting in a delay when it needs to spin back up u ...

Support for both Fetch API and XMLHttpRequest across browsers is imperative for seamless data retrieval and

Currently, I am diving into the world of ajax programming techniques. However, I recently discovered that the XMLHttpRequest is deprecated and now I must transition to using the Fetch API. The catch is, according to MDN, the Fetch API is still considered e ...

Navigating through Asp.net MVC Routes using Angular

My angular application is supported by .Net 4.6 and MVC5 and has routes like: /admin /admin/manageusers /admin/export When I navigate to these routes using client side routing from my asp.net application to /Admin, everything works fine. However, if I ...

Verify your identity using Google reCaptcha during Passport authentication

I am currently working on integrating google reCaptcha into my signup form's back-end, which already includes passport authentication. The code I have so far looks like this: app.get('/signup', function(req, res) { // render the ...