繁体   English   中英

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

[英]AngularJS Ng-Repeat - Multiple Custom Filters to Filter Items by JSON Object Properties

JSFiddle: http : //jsfiddle.net/ADukg/16368/

我正在尝试通过多个过滤器过滤ng-repeat列表中的项目,如果该对象的JSON属性与所选选项匹配。

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

我还希望能够将这两个过滤器组合在一起,以便当选择两个过滤器时,列表仅显示未读属性和高重要性属性都等于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>

您可以使用ng-show in li项检查两个条件(未读和highImportance)来实现这种功能。

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

});

然后,您必须在控制器方法中调用filter。

$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。 随时进行优化-根据需要更改我的示例实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM