Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to chain AngularJS filters in controller

Tags:

I have few filters in view

  <tr ng-repeat="x in list | filter:search| offset:currentPage*pageSize| limitTo:pageSize "> 

In my project to achieve good result, i have to make this filtering in controller not in view

i know the basic syntax $filter('filter')('x','x') but i don't know how to make chain of filters in controller, so everything will work as in my example from template.

I found some solution, now just with one filter, but should work with many ;)

       $scope.data = data; //my geojson from factory//         $scope.geojson = {}; //i have to make empty object to extend it scope later with data, it is solution i found for leaflet //         $scope.geojson.data = [];         $scope.FilteredGeojson = function() {         var result = $scope.data;         if ($scope.data) {           result = $filter('limitTo')(result,10);           $scope.geojson.data = result;           console.log('success');        }            return result;         }; 

and i use this function in ng-repeat works fine, but i have to check it with few filters.

like image 242
IOR88 Avatar asked Jan 09 '15 01:01

IOR88


People also ask

What is the correct way to apply multiple filters in AngularJS?

Filters can be applied to the result of another filter. This is called "chaining" and uses the following syntax: {{ expression | filter1 | filter2 | ... }} E.g. the markup {{ 1234 | number:2 }} formats the number 1234 with 2 decimal points using the number filter.

Which character is used to chain multiple filters in AngularJS?

The chaining filters are used to perform the multiple filter operations within the single result. This chaining filters operation will be chained using the pipe (|) symbol.

How do you use a filter on a controller?

You can load only a specific filter by appending the filter name with Filter . app. controller('MyController', function(uppercaseFilter) { // HELLO var text = uppercaseFilter('hello'); // Hello var text = uppercaseFilter('hello', true); });

Which is the correct way to apply a filter in AngularJS?

In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for the filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.


1 Answers

You can just re-filter what you get returned from your first filter. So on and so forth.

var filtered; filtered = $filter('filter')($scope.list, {name: $scope.filterParams.nameSearch}); filtered = $filter('orderBy')(filtered, $scope.filterParams.order); 

Below plunkr demonstrates the above.

http://plnkr.co/edit/Ej1O36aOrHoNdTMxH2vH?p=preview

like image 76
Angad Avatar answered Oct 21 '22 18:10

Angad