cjmling cjmling - 1 year ago 72
Javascript Question

AngularJS multiple filter with custom filter function

I am trying to filter the list with multiple filters + with a custom filter function.

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

I want to add a custom filter so that age it filter based on two input value which is min_age and max_age , (between age) .

After looking into doc. I found people having similar questions and a user Mark Rajcok answer http://docs.angularjs.org/api/ng.filter:filter#comment-648569667 looks good and should be working. But I am having problem applying it into my codes which mainly seems to because I have other multiple filters.

Im very new to AngularJS :(

My tried and NOT working fiddle is here http://jsfiddle.net/ed9A2/20/

A copy paste of my NOT working codes are here


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


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}

Answer Source

Try this:

<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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download