AngularJS multiple filter with custom filter function

I am trying to filter a list with multiple filters + using a special filter function.

The original working jsfiddle example is http://jsfiddle.net/ed9A2/1/ , but now I want to change the way the age is filtered.

I want to add a custom filter so that age is filtered based on the two input values min_age and max_age , (between the ages).

After viewing the document. I found people with similar questions, and Mark Rajcok user answers http://docs.angularjs.org/api/ng.filter:filter#comment-648569667 , which looks good and should work. But I had a problem with its application in my codes, this is mainly due to the fact that I have several different filters.

Im very new to AngularJS :(

My trial and broken fiddle is here http://jsfiddle.net/ed9A2/20/

Copy my my work codes here

View

<div ng-app ng-controller="MainController"> <table class="fancyTable"> <tr> <th>Player id</th> <th>Player name</th> <th>Age</th> </tr> <tr> <td><input ng-model="player_id" /></td> <td><input ng-model="player_name" /></td> <td> Min Age:<input ng-model="min_age" /> Max Age:<input ng-model="max_age" /> </td> </tr> <tr ng-repeat="player in players | filter:{id: player_id, name:player_name, age:ageFilter}"> <td>{{player.id}}</td> <td>{{player.name}}</td> <td>{{player.age}}</td> </tr> </table> 

controller

 function MainController($scope) { $scope.player_id = ""; $scope.player_name = ""; $scope.player_age = ""; $scope.min_age = 0; $scope.max_age = 999999999; $scope.ageFilter = function(player) { return ( player > $scope.min_age && player.age < $scope.max_age); } $scope.players = [ {"name": "Rod Laver", "id": "rod", "date": "1938/8/9", "imageUrl": "img/rod-laver.gif", "age": 75}, {"name": "Boris Becker", "id": "borix", "date": "1967/11/22", "imageUrl": "img/boris-becker.gif", "age": 45}, {"name": "John McEnroe", "id": "mcenroe", "date": "1959/2/16", "imageUrl": "img/john-mc-enroe.gif", "age": 54}, {"name": "Rafa Nadal", "id": "nadal", "date": "1986/5/24", "imageUrl": "img/ndl.jpg", "age": 27} ] } 
+89
javascript angularjs angularjs-ng-repeat
Sep 13 '13 at 17:30
source share
2 answers

Try the following:

 <tr ng-repeat="player in players | filter:{id: player_id, name:player_name} | filter:ageFilter"> $scope.ageFilter = function (player) { return (player.age > $scope.min_age && player.age < $scope.max_age); } 
+188
Sep 13 '13 at 17:37
source share

The hope below the answer in this link will help, Filter multiple values

And look at the fiddle with an example

 arrayOfObjectswithKeys | filterMultiple:{key1:['value1','value2','value3',...etc],key2:'value4',key3:[value5,value6,...etc]} 

fiddle

+7
Jan 16 '14 at 18:23
source share



All Articles