AngularJS NG -Repeat-多个自定义过滤器,可通过JSON对象属性过滤项目



jsfiddle:http://jsfiddle.net/adukg/16368/。

,我正在尝试通过多个过滤器在NG重复列表中过滤项目,如果其对象的JSON属性匹配了选定的选项。

因此,当选择"未读"过滤器时,仅在每个JSON对象的"未读"属性等于true的情况下显示项目,并且与高重要性过滤器类似。

我还希望能够组合这两个过滤器,以便列表仅在选择过滤器时显示出具有未读和高重要性属性的项目。

我在JSFiddle上有一个基本设置在这种情况下会做我需要的。

html:

<div>
  <label for="filterByAllCheckbox">Filter by All </label>
  <input ng-model="filters.all" ng-change="filterByAll()" type="checkbox" id="filterByAllCheckbox" ng-disabled="filters.all">
</div>
<div>
  <label for="filterByUnreadCheckbox">Filter by Unread </label>
  <input ng-model="filters.unread" ng-change="manageFilters()" type="checkbox" id="filterByUnreadCheckbox">
</div>
<div>
  <label for="filterByHighImportanceCheckbox">Filter by High Importance </label>
  <input ng-model="filters.highImportance" ng-change="manageFilters()" type="checkbox" id="filterByHighImportanceCheckbox">
</div>
<br>
<ul>
  <b>NOTIFICATIONS</b>
  <li ng-repeat="notification in notifications">
    {{notification.title}}
  </li>
</ul>

您可以在li项目中使用ng-show实现此类功能,以检查两个条件(未阅读和高级职位)。

<li ng-repeat="notification in notifications" 
    ng-show="(filters.all) 
    || (filters.unread && !notification.read && filters.highImportance && notification.importance == 'High')
    || (!filters.unread && notification.read && filters.highImportance && notification.importance == 'High')
    || (filters.unread && !notification.read && !filters.highImportance && notification.importance != 'High')
    || (!filters.unread && notification.read && !filters.highImportance && notification.importance != 'High')">
  {{notification.title}}
</li>

我怀疑这是否是实现您描述的最佳方法。

更新。实现自定义过滤器。

var myApp = angular.module('myApp',[]).filter('notificationFilter', function () {
    return function (array, all, unread, highImportance) {
        var matches = [];
        for (var i = 0; i < array.length; i++) {
            if (all 
                || (!unread && highImportance && array[i].importance == 'High')
                || (unread && !array[i].read && !highImportance)
                || (unread && !array[i].read && highImportance && array[i].importance == 'High')) {
            matches.push(array[i]);
        }
        return matches;
    };
});

然后您必须在控制器方法中调用过滤器。

$scope.manageFilters = function() {
    if ($scope.filters.unread == true || $scope.filters.highImportance == true) {
        $scope.filters.all = false;
    } else {
        $scope.filters.all = true;
    }
    $scope.shownNotifications = $filter('notificationFilter')($scope.notifications, $scope.filters.all, $scope.filters.unread, $scope.filters.highImportance);
}

$scope.filterByAll = function() {
    if ($scope.filters.all == true) {
        $scope.filters.unread = false;
        $scope.filters.highImportance = false;
        $scope.shownNotifications = $filter('notificationFilter')($scope.notifications, $scope.filters.all, $scope.filters.unread, $scope.filters.highImportance);
    }
}

当然更改html:

<li ng-repeat="notification in shownNotifications">
  {{notification.title}}
</li>

请确保在控制器定义中声明$filter并初始化列表shownNotifications。您可以在此处查看更新的JSFIDDLE。随意优化 - 根据需要更改我的示例实现。