Skip to content Skip to sidebar Skip to footer

Determining How Many Results Are Shown By Ng-repeat Filter

If I use ng-repeat with filter, how can I determine how many elements are filtered out (or vice versa, how many are shown)? Specifically, I would like to create a search bar that c

Solution 1:

Please see here http://jsbin.com/xuyuy/1/edit

In your repeater you can create new array ie : personsfiltered

ng-repeat="person in personsfilterd = (persons | filter : search )

and after that just display length of orignal array and filtered array :

Showing {{personsfilterd.length}} of {{persons.length }} results

Full code here:

var app = angular.module('app', []);

app.controller('fCtrl', function($scope) {

  $scope.persons = [
    'Mike', 'Tom', 'Tim', 'Jim', 'Ken'
  ]

});

 <divng-app="app"><divng-controller="fCtrl"><inputtype="text"ng-model = "search"> Showing {{personsfilterd.length}} of {{persons.length }} results
      <ling-repeat="person in personsfilterd = (persons | filter : search )">{{person}}</li></div></div>

Solution 2:

First, the ngRepeat has these: $index, $first, $middle, $last for determining the current position. So I guess you may be interested in the $last. What I'm not sure about is whether it will pick up the original count or the filtered one.

EDIT: You can obtain the count of original value from your ctrl, by setting the varX.length and using that in your template (HTML code) and then, assuming that $last is returning the final count+1 (as it's 0-based) you can have the count after filter is applied.

ngRepeat

Solution 3:

I found my answer here. The solution is to assign the filtered data to a new variable in the ng-repeat. Potentially could cause some memory issues, but not really relevant in my case.

Post a Comment for "Determining How Many Results Are Shown By Ng-repeat Filter"