Search code examples
angularjsangular-filters

AngularJS filter won't filter values that are equal to 0


I have the following search input:

<input type="text" ng-model="search.Kkz">

And I have the following ng-repeat:

<tr class="ListItem" ng-class-odd="'Odd'" ng-class-even="'Even'"
        ng-repeat="item in Prios | filter:search | filterMultiple:{Sprache:spracheSelectedItem} | orderBy:predicate:reverse | startFrom:currentPage*pageSize | limitTo:pageSize"
        ng-click="setSelectedRow(item)" ng-class="{'Selected': item == selectedRow}">

Currently the Kkz field can have 2 values (3000 and 0). When I search for the number 3000, it returns a correct list. But when I search for the value "0" nothing happens, as if it doesn't find that value in the table column that I am searching.


Solution

  • By default the filter looks for a case insensitive substring match. Here it finds the 0 within 3000 so both 3000 and 0 match.

    In the case of a simple array of strings you could use filter:search:true, in this case where search is an object creating your own comparator can help:

    In the HTML add the name of the compare function:

    ... | filter:search:compare | ...
    

    In the controller:

    $scope.compare = function (actual, expected) {
      // !expected: All records pass when no search value is specified.
      // == instead of ===: Coerce the numbers from the array into strings to 
      //                    properly compare to the string from the input. 
        return (!expected || actual == expected);
    };