AngularJS filter with multiple parameters

I want to be able to filter my table with many parameters passed as an array. Therefore, I can create an array of filter parameters and pass them. I do not want to explicitly indicate which columns to filter, since there can be many columns (some of them will be shown, and some not).

HTML looks something like this:

<tr ng-repeat="item in infoData | filter:['param1','param2']"> <td>{{item.data1}}</td> <td>{{item.data2}}</td> <td>{{item.data3}}</td> <td>{{item.data4}}</td> </tr> 

Is it possible to filter the table against several parameters?

thanks

+5
source share
1 answer

This is a quick and dirty way to accomplish what you need.

First, create a custom filter in the controller like this:

 $scope.customFilter = function(param1, param2) { return function(item) { //return custom stuff here } } 

then in html you do it

 <tr ng-repeat="item in infoData | filter:customFilter(param1, param2)"> <td>{{item.data1}}</td> <td>{{item.data2}}</td> <td>{{item.data3}}</td> <td>{{item.data4}}</td> </tr> 

this is an example with a custom filter

 app.filter('customFilter', function (param1, param2) { return function (item) { //return custom stuff here }; }); 

and now in html you do the following:

 <tr ng-repeat="item in infoData | customFilter(param1, param2)"> <td>{{item.data1}}</td> <td>{{item.data2}}</td> <td>{{item.data3}}</td> <td>{{item.data4}}</td> </tr> 
+5
source

Source: https://habr.com/ru/post/1202597/


All Articles