Search code examples
angularjsangular-filters

How to chain AngularJS filters in controller


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.


Solution

  • 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