具有自定义过滤功能的 AngularJS 多重过滤器 [英] AngularJS multiple filter with custom filter function

查看:45
本文介绍了具有自定义过滤功能的 AngularJS 多重过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多个过滤器 + 使用自定义过滤器功能过滤列表.

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

我想添加一个自定义过滤器,以便 age 它根据两个输入值进行过滤,即 min_agema​​x_age ,(在年龄之间) .

在查看文档后.我发现有人有类似的问题,用户 Mark Rajcok 回答了 http://docs.angularjs.org/api/ng.filter:filter#comment-648569667 看起来不错,应该可以工作.但是我在将它应用到我的代码中时遇到了问题,这似乎主要是因为我有其他多个过滤器.

我对 AngularJS 很陌生:(

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

我的无效代码的复制粘贴在这里

查看

<table class="fancyTable"><tr><th>玩家id</th><th>玩家姓名</th><th>年龄</th></tr><tr><td><input ng-model="player_id"/></td><td><input ng-model="player_name"/></td><td>最小年龄:最大年龄:<input ng-model="max_age"/></td></tr><tr ng-repeat="玩家中的玩家 | filter:{id: player_id, name:player_name, age:ageFilter}"><td>{{player.id}}</td><td>{{player.name}}</td><td>{{player.age}}</td></tr>

控制器

function MainController($scope) {$scope.player_id = "";$scope.player_name = "";$scope.player_age = "";$scope.min_age = 0;$scope.max_age = 999999999;$scope.ageFilter = 函数(播放器){return ( player > $scope.min_age && player.age < $scope.max_age);}$scope.players = [{"name": "罗德紫菜","id": "杆","日期": "1938/8/9","imageUrl": "img/rod-laver.gif",年龄":75},{"name": "鲍里斯·贝克尔","id": "硼砂","日期": "1967/11/22","imageUrl": "img/boris-becker.gif",年龄":45},{"name": "约翰麦肯罗","id": "mcenroe","日期": "1959/2/16","imageUrl": "img/john-mc-enroe.gif",年龄":54},{"name": "拉法纳达尔","id": "纳达尔","日期": "1986/5/24","imageUrl": "img/ndl.jpg",年龄":27}]}

解决方案

试试这个:

$scope.ageFilter = 函数(播放器){返回 (player.age > $scope.min_age && player.age < $scope.max_age);}

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

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

解决方案

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

这篇关于具有自定义过滤功能的 AngularJS 多重过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆