AngularJS多重过滤器,具有自定义过滤功能 - 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. 最初的工作jsfiddle示例是http://jsfiddle.net/ed9A2/1/,但现在我想改变年龄过滤方式。

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) . 我想添加自定义过滤器,以便年龄段正是过滤器的基础上这是MIN_AGEMAX_AGE,(年龄之间)两个输入值。

After looking into doc. 在研究了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. 我发现人们有类似的问题和用户Mark Rajcok回答http://docs.angularjs.org/api/ng.filter:filter#comment-648569667看起来不错,应该可以正常工作。 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 :( 我对AngularJS很新:(

My tried and NOT working fiddle is here http://jsfiddle.net/ed9A2/20/ 我试过而且不工作的小提琴就在这里http://jsfiddle.net/ed9A2/20/

A copy paste of my NOT working codes are here 我的NOT工作代码的复制粘贴在这里

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}
    ]
}

解决方案:

参考: https://stackoom.com/en/question/1GqfP
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章