3

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?

1

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.