如何在Angular ng-repeat中按类别键过滤数组中的对象

时间:2022-08-23 23:36:49

I'm trying to use Angular filter to display only sorted tags by category

我正在尝试使用Angular过滤器按类别仅显示已排序的标签

Example of a tag object in tags array:

标签数组中的标签对象示例:

{
    term: term,
    id: id,
    category: category
}

The ng-repeat tags:

ng-repeat标签:

<li ng-repeat="(k, m) in tags | filter: filterTags | orderBy:predicate:reverse"
    ng-class="{'selected': m.selected}"
    ng-click="selectTag(m)">    
    <div class="tag">{{m.term}}</div>
</li>

The sort by category radio buttons:

按类别排序单选按钮:

如何在Angular ng-repeat中按类别键过滤数组中的对象

<div class="category-selection">
    <ul>
        <li>
            <input type="radio" ng-model="catSort" name="brand" value="brand">
            Brand
        </li>
        <li>
            <input type="radio" ng-model="catSort" name="client" value="client">
            Client
       </li>

In the sort radio button directive controller:

在sort单选按钮指令控制器中:

// Detect category sort
// Then apply the value to the filter function:

$scope.$watch('catSort', function(value) {
    console.log(value);
    tagsPanel = ScopeFactory.getScope('tagsPanel');
    tagsPanel.filterTags(value);
}); 

I found out that filter is has it's own Angular module, so my question is, how do I get the category strings into this filter?

我发现过滤器有它自己的Angular模块,所以我的问题是,如何将类别字符串放入此过滤器?

.filter('filterTags', function() {
    return function(tags, category) {
        return tags;
    };
});

Here is where I capture the new category, how would I send the value into the filter above?

这是我捕获新类别的位置,如何将值发送到上面的过滤器中?

$scope.$watch('catSort', function(value) {
    console.log(value);
}); 

1 个解决方案

#1


If I got it right. You want to filter your tag object array by category.

如果我做对了您希望按类别过滤标记对象数组。

You can call a scope method and return true if it matches the currently selected category. The parameter for this method will be a tag object of your ng-repeat. So you can do a check like return tag.category == $scope.catSort;

您可以调用范围方法,如果它与当前选定的类别匹配,则返回true。此方法的参数将是ng-repeat的标记对象。所以你可以像return tag.category == $ scope.catSort一样进行检查;

Please have a look at the demo below and here at jsFiddle.

请看下面的演示和jsFiddle。

(I've took sport categories just to have some dummy data.)

(我把运动类别只是为了得到一些虚拟数据。)

angular.module('myApp', [])
    .controller('mainCtrl', function ($scope) {
    $scope.catSort = "football";
    $scope.tags = [{
        term: 'foot1',
        id: 'id',
        category: 'football'
    }, {
        term: 'foot2',
        id: 'id2',
        category: 'football'
    }, {
        term: 'base1',
        id: 'id',
        category: 'baseball'
    }, {
        term: 'base2',
        id: 'id2',
        category: 'baseball'
    }, ];

    $scope.filterTags = function (tag) {
        //console.log(tag, $scope.catSort);
        return tag.category == $scope.catSort;
    };
});
ul {
    list-style-type: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="mainCtrl">Filter by category:
    <div class="category-selection">
        <ul>
            <li>
                <input type="radio" ng-model="catSort" name="football" value="football" />Football</li>
            <li>
                <input type="radio" ng-model="catSort" name="baseball" value="baseball" />Baseball</li>
        </ul>
    </div>Teams:
    <ul>
        <li ng-repeat="(index, tag) in tags | filter: filterTags" ng-class="{'selected': tag.selected}" ng-click="selectTag(tag)">
            <div class="tag">{{tag.term}}</div>
        </li>
    </ul>
</div>

#1


If I got it right. You want to filter your tag object array by category.

如果我做对了您希望按类别过滤标记对象数组。

You can call a scope method and return true if it matches the currently selected category. The parameter for this method will be a tag object of your ng-repeat. So you can do a check like return tag.category == $scope.catSort;

您可以调用范围方法,如果它与当前选定的类别匹配,则返回true。此方法的参数将是ng-repeat的标记对象。所以你可以像return tag.category == $ scope.catSort一样进行检查;

Please have a look at the demo below and here at jsFiddle.

请看下面的演示和jsFiddle。

(I've took sport categories just to have some dummy data.)

(我把运动类别只是为了得到一些虚拟数据。)

angular.module('myApp', [])
    .controller('mainCtrl', function ($scope) {
    $scope.catSort = "football";
    $scope.tags = [{
        term: 'foot1',
        id: 'id',
        category: 'football'
    }, {
        term: 'foot2',
        id: 'id2',
        category: 'football'
    }, {
        term: 'base1',
        id: 'id',
        category: 'baseball'
    }, {
        term: 'base2',
        id: 'id2',
        category: 'baseball'
    }, ];

    $scope.filterTags = function (tag) {
        //console.log(tag, $scope.catSort);
        return tag.category == $scope.catSort;
    };
});
ul {
    list-style-type: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="mainCtrl">Filter by category:
    <div class="category-selection">
        <ul>
            <li>
                <input type="radio" ng-model="catSort" name="football" value="football" />Football</li>
            <li>
                <input type="radio" ng-model="catSort" name="baseball" value="baseball" />Baseball</li>
        </ul>
    </div>Teams:
    <ul>
        <li ng-repeat="(index, tag) in tags | filter: filterTags" ng-class="{'selected': tag.selected}" ng-click="selectTag(tag)">
            <div class="tag">{{tag.term}}</div>
        </li>
    </ul>
</div>