Is the custom comparator in AngularJS filter not functioning properly with null values?

[New Version] I've discovered a solution to address this issue - by utilizing AngularJs version 1.3.6 or higher, items with null properties will not vanish after applying a filter and then removing it!

original query below

Hello everyone, I am currently using AngularJs to exhibit data in a table using the ng-repeat directive, along with a textbox for filtering the "Color" property of cats. It's worth noting that some cats have a null Color property.

Whenever I enter text in the textbox and then clear it, all cats with a null Color property disappear.

I attempted setting a custom comparator that returns true when expected === '', but unfortunately, it did not work as expected. Essentially, cats with a Color property === null are never displayed once the search textbox has been filled with text and subsequently cleared, even if the comparator always returns true.

Is there any method to allow null values while using a filter in ng-repeat? Thank you in advance!!

JS Fiddle Demo: http://jsfiddle.net/gLL7wvcL/2/

my cat data (array of cats)

$scope.cats = [
{'Name': 'John',
 'Sex': 'Male',
 'Color': 'Black'},
{'Name': 'Rose',
 'Sex': 'Female',
 'Color': 'White'},
  {'Name': 'Cindy',
 'Sex': 'Female',
 'Color': 'Yellow'},
  {'Name': 'David',
 'Sex': 'Male',
 'Color': 'Black'},
  {'Name': 'Garen',
 'Sex': 'Male',
 'Color': null},
  {'Name': 'Jim',
 'Sex': 'Male',
 'Color': null},
  {'Name': 'Charotte',
 'Sex': 'Female',
 'Color': 'Black'}
];

Both comparators provided above are not effective; once the search textbox is filled with text and then cleared, all cats with a Color property equal to null are no longer visible.

$scope.myComparator = function (actual, expected) 
  {
      if (expected === '') 
          return true;
      else
      {
          var text = ('' + actual).toLowerCase();
            return text.indexOf(expected) > -1;
      }
  };

$scope.myComparator = function (actual, expected) 
  {
      return true;
  };

Answer №1

The issue at hand arises when the input is cleared, resulting in a filter model that looks like this:

{"Color": ""}

Consequently, an empty string will not match with colors having a null value. The solution involves removing the "Color" property if it is null, possibly upon change. This eliminates the need for the myComparator altogether:

$scope.clear = function() {
    if ($scope.search.Color === null) {
        delete $scope.search.Color;
    }
};

And then you can simply use:

ng-repeat="cat in cats | filter:search"

Check out the demo here: http://plnkr.co/edit/NswRd71q2UxG5KjZoL4Y?p=preview

Answer №2

One way to approach this is by utilizing a filter function that compares the entire object instead of just a specific property, and then linking the criteria to the controller scope.

Code Example:

<tr ng-repeat="cat in cats | filter:myComparator">

Controller Logic:

$scope.myComparator = function (actual) 
{
    var expected = $scope.filter;
    if (expected === '') 
    {
        return true;
    }
    else
    {
        var text = ('' + actual.Color).toLowerCase();
        return text.indexOf(expected) > -1;
    }
};

You can view a complete example on this updated fiddle.

Answer №3

When checking for an empty string, use the condition if (expected === '') instead of null.

You can also try using the simpler condition if (!expected) .....

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

Using .done(), .then(), and .when() in jQuery for sequencing ajax requests in a specific order

After diving into the world of Promises in jquery and trying to avoid "callback hell" when dealing with multiple ajax requests, I still find myself without a clear answer on which method to use - whether it's .done(), .then(), or .when() - for chainin ...

Having trouble retrieving nested object properties within a single object when making a JSON call in React?

When making a Rails API call, I receive a single JSON object with a nested User Object and a tag list array. However, I am unable to access the nested Object. The error message "Cannot read property 'name' of undefined" appears when trying to ac ...

Steps to update the active state in the "reducer" when clicking on elements outside of the "reducer" area

Working with react-redux has presented some challenges for me. I've created multiple reducers such as action, root reducer, active_step reducer, and list_step reducer. One aspect that I find intriguing is the ability to dynamically map and change the ...

What is the best way to access a key from an object within the map function?

What is the method to extract the key from an object in ReactJS? {this.state.data.map((object, index) => ( <div>{Object.keys(object)}</div> ))} For example, if this.state.data contains: [{mykey1:23},{mykey2:24},{mykey3:34}] T ...

Retrieve information from a database using AngularJS for a form

When visiting the front page, you will see two options: 1. Create New 2. Edit If you choose option 1, it will take you to index.html. Option 2 should navigate to index.html with pre-loaded data from the database. // Retrieving data from the database ...

What happens when you click and trigger mouseleave?

window.onload = function() { $(".compartir").hover(function() { console.log('hover'); var self = this; setTimeout($(self).addClass('ready'), 500); }, function() { var self = this; console.log('leave'); ...

eliminate the gap in the MUI Accordion when it is expanded

How can I prevent the Accordion MUI component from moving and applying top and bottom margins to summary elements in expanded mode? I added the following code to the summary element but it doesn't seem to be working. Any suggestions on how to fix this ...

Continue executing code until the Button is clicked once more

I was learning some new concepts and had a question that I couldn't find an answer to online. So, here's the button I have: <button onClick={toggle} className="common-btn">{buttonText}</button> I want this button to contr ...

Issues with AJAX requests failing to fire with the latest version of jQuery

A small script I have checks the availability of a username in the database, displaying "taken" if it's already taken and "available" if it's not. The code below works perfectly with jQuery v1.7.2. However, I need to update it for jQuery v3.2.1. ...

Error: The object "request" has not been defined. This issue occurs when trying to execute an Action

$dd = "<a href='javascript:void(0);' id='requestsend$send_id' onClick='request($send_id,request_send);' class='post-add-icon inline-items'><button type='button' style='background: #ccc;' ...

What is the best way to refresh a PHP function based on a JavaScript event?

I have a website where a div tag shows all the values from a SQL database using PHP. I want to reload this div tag with a JavaScript event, such as clicking on an HTML button. Is it possible to bring back all the values from the SQL database and display th ...

Python code allowing users to navigate back to the previous page while retaining elements

After my script scrapes the page, it automatically clicks a button if a new element meeting certain criteria is found. Everything works perfectly when there is only one element, but an issue arises when the button click leads to a new page opening. If ther ...

The collapsible list feature that includes the use of plus and minus signs is malfunctioning

Below is the script that I wrote to address this issue, but for some reason the + and - swapping is not functioning correctly. $('.showCheckbox').click(function(e) { var dynamicBox = $(this).attr('val'); var collapseSign = $(th ...

Determine the year immediately preceding the current date

I'm looking to delete records from MongoDB using JavaScript by passing a query. The dates in my database are in the format: "12 Feb 2019 06:45:34 GMT" and I need to retrieve the dates from 1 year ago for deletion. How can I achieve this? var d = n ...

Step-by-step guide on eliminating the modal post-validation

Newbie in reactjs looking for help with modal validation issue. Goal: Press submit button inside modal, validate, then close the modal. Want to reuse modal for another row. Problem: I'm having trouble making the function work for a new row after ...

Guide to fixing Vue app crashes caused by Web Sockets error

I'm encountering an issue with my Vue application as I am unable to load a specific page due to the app crashing. The console displays the following error, even though there is no implementation of Web Sockets in the app. Despite adding this snippet ...

Toggle the Bootstrap navbar class based on the current scroll position

I'm currently working on the development of a website that includes a feature where a static navbar transforms into a fixed navbar after scrolling down 500px. The transition from "navbar-static-top" to "navbar-fixed-top" is functioning properly. Howev ...

unselect the button using jQuery

I want to trigger a popover when a variable exceeds a certain value, triggered by clicking a button: $(function () { $('[data-toggle="tooltip"]').tooltip({ trigger: 'manual', placement: 'top', titl ...

When a specific item is selected from a drop-down menu, text boxes and drop-downs will dynamically appear and change

In the current version of my code, there is a single drop-down menu with three options for the user to select from. If "Complete" is chosen, a text box should appear. If "Abandon" or "Transfer" is selected, a separate drop-down menu needs to be displayed. ...

Running the Jcrop function multiple times within another function will not result in execution

When I click on the 'Edit A' link, a window opens where I can use Jcrop for editing. The process works smoothly for the first image. However, when I try to edit the second image using the 'Edit B' link, it keeps displaying the first ima ...