AngularJS多重过滤器,具有自定义过滤功能

问题描述 投票:92回答:3

我试图使用多个过滤器+使用自定义过滤器功能过滤列表。

最初的工作jsfiddle例子是http://jsfiddle.net/ed9A2/1/但现在我想改变年龄过滤方式。

我想添加一个自定义过滤器,以便它根据两个输入值(即min_age和max_age)(年龄之间)进行过滤。

在研究了doc之后。我发现人们有类似的问题和用户Mark Rajcok回答http://docs.angularjs.org/api/ng.filter:filter#comment-648569667看起来很好,应该工作。但我有问题将其应用到我的代码中,这主要是因为我有其他多个过滤器。

我对AngularJS很新:(

我试过而且不工作的小提琴就在这里http://jsfiddle.net/ed9A2/20/

我的NOT工作代码的复制粘贴在这里

视图

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

调节器

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}
    ]
}
javascript angularjs angularjs-ng-repeat
3个回答
194
投票

试试这个:

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

7
投票

希望以下答案在这个链接将有所帮助,Multiple Value Filter

并通过示例看看小提琴

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

fiddle


0
投票

在视图文件(HTML或EJS)中

<div ng-repeat="item in vm.itemList  | filter: myFilter > </div>

和控制器

$scope.myFilter = function(item) {
return (item.propertyA === 'value' || item.propertyA === 'value');
}
© www.soinside.com 2019 - 2024. All rights reserved.