Here is a small example of my html using ng-repeat:

<div ng-repeat="item in vm.templateList | filter: vm.myFilter">
   <h3>{{item.Code}}</h3>
</div>

In Js file the vm.templateList is as followed(as an example):

vm.templateList = [{Code: 'a', ID: 1}, 
                   {code: 'a', ID: 2},
                   {code: 'b', ID: 3},
                   {code: 'c', ID: 4}];

Imagine I want to filter this list for all items that have ID 1 and also items that have ID 2.

What I originaly was doing was like this:

vm.filter = {ID: 1};

But this was I can only filter the list on 1 ID. Can anyone suggest a way?

share|improve this question
    
You can create a custom filter – yarons Jan 10 at 15:55
up vote 1 down vote accepted

You can add the following AngularJS filter to your application :

// add a custom filter to your module
angular.module('MyModule').filter('myFilter', function() {
    // the filter takes an additional input filterIDs
    return function(inputArray, filterIDs) {
        // filter your original array to return only the objects that
        // have their ID in the filterIDs array
        return inputArray.filter(function (entry) {
            return this.indexOf(entry.ID) !== -1;
        }, filterIDs); // filterIDs here is what "this" is referencing in the line above
    };
});

You then declare your filter array in the controller as such :

vm.IDs = [1, 2];

Then your view should look like this :

<div ng-repeat="item in vm.templateList | myFilter: vm.IDs">
    <h3>{{item.Code}}</h3>
</div>
share|improve this answer
    
Thanks it worked :) – Marjan Kalanaki Jan 11 at 18:12
    
You are most welcome. – Ghassen Louhaichi Jan 11 at 18:19

You can use something like:

html:

<section>
    <div ng-repeat="item in vm.templateList | filter:checkFilterOptions">
       <h3>{{item.Code}}</h3>
    </div>
</section>

Js:

    $scope.vm = {};
    $scope.vm.templateList = [
                    {Code: 'a', ID: 1},
                    {Code: 'a', ID: 2},
                    {Code: 'b', ID: 3},
                    {Code: 'c', ID: 4}
                    ];

    $scope.filterOptions = [1,2,3];

    $scope.checkFilterOptions = function(value, index) {
      return value.ID && $scope.filterOptions.indexOf(value.ID) !== -1;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.