What methods can be used to deeply filter in Angular using the 'filter' function?

Let's imagine I have various items stored in an array:

app.controller('ItemsController', function() {
        this.items = [
            {},
            {
                name: 'apple',
            },
            {
                name: 'banana',
                color: null,  
            },
            {
                name: 'orange',
                color: { red: true, yellow: true },
            },
            {
                name: 'grape',
                color: { red: true, purple: false },
            },
            {
                name: 'watermelon',
                color: { red: false, green: true },
            },
            {
                name: 'kiwi',
                color: { red: false, green: false },
            },
        ];
    });
    

I would like to display these items and have the ability to filter the list based on certain criteria. For example, I might only want to display items with a specific name or those with a certain color attribute set to true. To achieve this, I link it to the following HTML structure:

<div ng-init="filterObj = {};">
        Name: <input type="text" ng-model="filterObj.name" />
        Red: <input type="checkbox" ng-model="filterObj.color.red" />
        Yellow: <input type="checkbox" ng-model="filterObj.color.yellow" />
        <input type="reset" ng-click="filterObj = {};" />

        <ul ng-controller="ItemsController as ctrl">
            <li ng-repeat="item in ctrl.items | filter: filterObj"><!-- content --></li>
        </ul>
    </div>
    

While filtering by name works correctly, filtering by color does not yield the expected results, which can be seen in this jsfiddle. Setting either of filterObj.color.red orfilterObj.color.yellow to true or false causes the filter to match any truthy values under color. Filtering using filter: filterObj : true also does not solve the issue since it filters based on exact deep equality (i.e.,

filterObj = { color: { red: true } }
wouldn't match any of the listed items above).

How can I implement deep filtering so that

filterObj = { color: { red: true } }
matches items where the name is 'orange' or 'kiwi' but not others? Would I need to create a custom filter or comparator, or is there a workaround that I could utilize?

Answer №1

I managed to make it work by using a custom comparator. It became even easier when I discovered that there's an underscore function designed for this specific comparison.

app.controller('ThingsController', function() {
    this.things = [ /* ... */ ];

    this.filterComparator = function(actual, expected) {
        return _.matches(expected)(actual);
    };
});

HTML:

<li ng-repeat="thing in ctrl.things | filter: filterObj : ctrl.filterComparator"><!-- content --></li>

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

Navigating through a JSON dictionary in Svelte: A step-by-step guide

When working with Svelte, the #each construct allows for easy iteration over array-like objects. But what if you have a JSON dictionary object instead? Is there a way in Svelte to iterate over this type of object in order to populate a dropdown menu? The ...

Having trouble retrieving data sent via ajax in PHP

Currently, I am using Ajax to send a variable in my PHP file. Here's the code snippet: getVoteCount: function(){ App.contracts.Election.deployed().then(function(instance) { for(i=0; i<4; i++){ instance.candidates(i).then(functi ...

Is there a way to show a loading icon during the image change process without using jQuery?

How can I add a loading icon to an image change event without using jQuery? I'm new to the coding world and Stack Overflow community, so please bear with me. I've been searching for a solution to my specific situation but haven't been able ...

Encountered a CSS error while trying to compile the project with npm build

When attempting to build the project, I encountered a postcss error. After some debugging, I discovered that the imports below were causing the issue: @import "@material/button/dist/mdc.button.min.css"; /*material text box css*/ @import "@material/float ...

Excluding specific e2e tests in Protractor: A guide

I have a collection of end-to-end tests for my AngularJS web application. Here is the configuration in my current protractor.config.js file: // __dirname fetches the path of this specific config file // assuming that the protractor.conf.js is located at t ...

"PHP Dilemma: Navigating the Ajax Button Press Situation

I've been attempting to create a button that triggers a PHP script on a webpage and updates only a specific div tag, but so far I haven't had any success. Removing the $ajax section of the script allows my buttons to change states, but as soon as ...

Are the orders of events maintained when emitted using an event emitter?

I have a simple query regarding the event emitter, which is crucial for my program's logic. Currently, I am utilizing an external library that triggers events that I'm listening to. These events consist of 'data' and 'error'. ...

Tips for navigating through the search results on Google Maps with Selenium

I am facing an issue where I am only able to retrieve the first 7 results out of 20 using the given code. It seems that I am unable to scroll to the bottom to access all the results. Can someone please advise on additional steps required to achieve the d ...

Challenges with Performance in IONIC Mobile Applications

Currently, we are in the final stages of developing a high-profile mobile application for one of our clients using the IONIC framework. The application performs well when accessed through a Web/Mobile Browser. However, once it is ported into a mobile appli ...

Iterate over an ASP.Net table using a loop

Currently, I am facing a challenge with looping through a table that is dynamically created using C#. I have a client-side checkbox that should trigger the loop each time it's clicked. While I believe I can manage the looping process itself, I am enco ...

Scroll to the top of a new page with Material UI's table component

Feeling a little stuck here. I've tested various settings with scrollTop, but haven't had much luck. To clarify, I'm working with Material UI and their pagination features in conjunction with a table (you can find the documentation here). W ...

How come .trim() isn't cooperating with me?

I am encountering an issue with this particular piece of javascript. Every time I attempt to use it, nothing is displayed in my div. Instead, it simply adds ?weight=NumberInputed&measure=lbsOrkgs&submit=Submit to the URL. <h2>What size d ...

Waiting for the listener script to finish its task in an Ajax call and then informing the user

I have developed a unique program that allows users to submit test cases from a specific webpage (main.php). The webpage triggers an ajax request to insert user data into MySQL using the file insert.php, with the value done=0. Subsequently, there is a list ...

Retrieve the AngularJS form object using ng-click without requiring the model name to be passed

Within my application, I am dealing with several forms that have distinct names. I am looking for a way to access the angular form object within a function triggered by an ng-click event. Here is an example: https://jsfiddle.net/EvgeniiMalikov/fpt5kcba/ ...

The Cordova Camera plugin encounters a problem stating that the "getPicture" method is not found in the object

While working on an Ionic v1 Camera app, I encountered an error after setting up everything correctly. The plugin was installed properly, ng-cordova.min was included, and $CordovaCamera was injected. However, when I tried to take a photo using the followin ...

How can I modify the mesh structure in Three.js?

Having two meshes, mesh1 and mesh2, each with the same number of vertices and extrusion. mesh1 = 5000 vertices. mesh2 = 5000 vertices. I transfer the vertices from mesh2 to mesh1. Then I execute: mesh2.geometry.verticesNeedUpdate = true; mesh2.geometry. ...

The gif loader persists even after subscribing

Looking to incorporate the SendGrid subscription widget into my site, but struggling with the implementation. The code provided by SendGrid is partially functional - the loader appears and a success message is displayed upon sign up, but the GIF loader doe ...

Uh-oh! Angular 6 has encountered an unexpected error with template parsing

I am currently working on an Angular application where I have integrated the FormsModule from '@angular/forms' in my app.module.ts. However, despite this, I keep encountering the error message No provider for ControlContainer. Error log: Uncaug ...

Troubleshooting the issue of req.body being undefined in Node.js

I am having trouble with my node js server as I keep receiving undefined in the output of req.body when trying to request SQL data from a form Get request. Although I can successfully retrieve other queries, I'm unable to do so for requests involving ...

Utilize a Chrome extension to emphasize text within a contenteditable element by underlining it

I am in the process of developing a Chrome extension that highlights specific words when a user types them. For example, if a user inputs "hello," I want to highlight or underline it. The challenge arises when dealing with content editable divs in platform ...