view raw
cjmling cjmling - 9 months ago 41
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 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 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

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}


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);