简体   繁体   English

AngularJS使用一个对象作为另一个的过滤器

[英]AngularJS use one object as a filter for another

I'm trying to filter my ng-repeat through a set of checkboxes which come from a different object. 我正在尝试通过一组来自不同对象的复选框来过滤ng-repeat。 Object 1 holds my categories and object 2 holds all my articles . 对象1保存我的类别 ,对象2保存我的所有文章

The categores object will turn into checkboxes. 类别对象将变成复选框。 These checkboxes should act as filter for the articles . 这些复选框应充当文章的过滤器。 An article can have mutliple categories. 文章可以具有多个类别。

$scope.categories: $ scope.categories:

[
  {
    "id": "1",
    "title": "Blog"
  },
  {
    "id": "2",
    "title": "News"
  }
]

$scope.articles: $ scope.articles:

[
  {
    "id": "1",
    "title": "Whats going on",
    "categories":{
     "results" : [1,2,3]
    }
  },
  {
    "id": "2",
    "title": "Trump!",
    "categories":{
     "results" : [3]
    }
  }
]

Checkboxes: 复选框:

<div class="filter-pills" ng-repeat="cat in categories">
   <input type="checkbox" ng-model="filter[cat.Id]" ng-checked="cat.checked"/>{{cat.Title}}                         
</div>

ng-repeat: NG-重复:

<div class="col-lg-3 col-md-4" ng-repeat="item in articlesFinal"></div>

I have tried different solutions like ng-change when i update my filter array and compare it to the object used in ng-repeat . 更新filter数组并将其与ng-repeat使用的对象进行比较时,我尝试了ng-change类的其他解决方案。

I can't seem to figure this one out. 我似乎无法弄清楚这一点。 Any suggestions? 有什么建议么?

Try this 尝试这个

<div class="filter-pills" ng-repeat="cat in categories">
      <input type="checkbox" ng-model="cat.checked"/>{{cat.title}}
</div>
<div class="col-lg-3 col-md-4" ng-repeat="item in articles | filter: catFilter">{{item.title}}</div>

and in controller 并在控制器中

$scope.catFilter = function (article) {
        var checkedCats = vm.categories.filter(function (cat) {
            return cat.checked;
        });
        // no filter, show all
        if(checkedCats.length == 0) return true;

        for(var i = 0; i < checkedCats.length; i++){
            var id = checkedCats[i].id;
            if(article.categories.results.indexOf(id) >= 0){
                return true;
            }
        }
        // no match, then false
        return false
    };

Also notice that category id should be integer, not string 还要注意类别ID应该是整数,而不是字符串

$scope.categories = [
            {
                "id": 1, // integer
                "title": "Blog"
            },
            {
                "id": 2,
                "title": "News"
            }
        ];
        $scope.articles = [
            {
                "id": "1",
                "title": "Whats going on",
                "categories":{
                    "results" : [1,2,3] // integer
                }
            },
            {
                "id": "2",
                "title": "Trump!",
                "categories":{
                    "results" : [3]
                }
            }
        ];

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

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