How to display length of filtered ng-repeat data

angularjs count items in object
ng-show filter length
angularjs filter array length
ng-repeat filter
angularjs filter multiple values
angularjs filter'(array contains)
ng-repeat length of array
ng-options filter

I have a data array which contains many objects (JSON format). The following can be assumed as the contents of this array:

var data = [
  {
    "name": "Jim",
    "age" : 25
  },
  {
    "name": "Jerry",
    "age": 27
  }
];

Now, I display these details as:

<div ng-repeat="person in data | filter: query">
</div

Here, query is modeled to an input field in which the user can restrict the data displayed.

Now, I have another location in which I display the current count of people / person being display, i.e Showing {{data.length}} Persons

What I want to do is that when the user searches for a person and the data displayed is filtered based on the query, the Showing...persons also change the value of people being shown currently. But it is not happening. It always displays the total persons in data rather than the filtered one - how do I get the count of filtered data?

For Angular 1.3+ (credits to @Tom)

Use an alias expression (Docs: Angular 1.3.0: ngRepeat, scroll down to the Arguments section):

<div ng-repeat="person in data | filter:query as filtered">
</div>

For Angular prior to 1.3

Assign the results to a new variable (e.g. filtered) and access it:

<div ng-repeat="person in filtered = (data | filter: query)">
</div>

Display the number of results:

Showing {{filtered.length}} Persons

Fiddle a similar example. Credits go to Pawel Kozlowski

How to display length of filtered ng-repeat data in AngularJS , How to display length of filtered ng-repeat data in AngularJS ? Approach: To display the length of filtered ng-repeat data we use an alias expression. We create an alias for the variable used to filter the ng-repeat data and display the length of the alias at each moment. Syntax: Example: Explanation: Output: Approach: To display the length of filtered ng-repeat data we use an alias expression. We create an alias for the variable used to filter the ng-repeat data and display the length of the alias at each moment.

How to Show Angularjs Filtered Count, If you use AngularJs lists with ngRepeat directive and set a filter on it, you probably data-ng-bind="user.name"></li> </ul> <h3>{{ usersList.length | number }}  Now, I have another location in which I display the current count of people/person being display, i.e Showing {{data.length}} Persons What I want to do is that when the user searches for a person and the data displayed is filtered based on the query, the Showingpersons also change the value of people being shown currently.

The easiest way if you have

<div ng-repeat="person in data | filter: query"></div>

Filtered data length

<div>{{ (data | filter: query).length }}</div>

How to display length of filtered ng-repeat data, To display the length of filtered ng-repeat data the easiest way would be to use the below-mentioned way:- <div ng-repeat="person in data  Assuming your list of people is in data variable and you filter people using query model, the following code will work for you: <p>Number of visible people: {{(data|filter:query).length}}</p> <p>Total number of people: {{data.length}}</p> {{data.length}} - prints total number of people {{(data|filter:query).length}} - prints filtered number of people

ngRepeat creates a copy of the array when it applies a filter, so you can't use the source array to reference only the filtered elements.

In your case, in may be better to apply the filter inside of your controller using the $filter service:

function MainCtrl( $scope, filterFilter ) {
  // ...

  $scope.filteredData = myNormalData;

  $scope.$watch( 'myInputModel', function ( val ) {
    $scope.filteredData = filterFilter( myNormalData, val );
  });

  // ...
}

And then you use the filteredData property in your view instead. Here is a working Plunker: http://plnkr.co/edit/7c1l24rPkuKPOS5o2qtx?p=preview

Tip: accessing filtered array outside ng-repeat, Let's say I have an array of people and I filter them on a ng-repeat , then Having that in mind, we can now access its length property to show a  possible duplicate of How to display length of filtered ng-repeat data – Damjan Pavlica Jul 24 '15 at 11:35

Since AngularJS 1.3 you can use aliases:

item in items | filter:x as results

and somewhere:

<span>Total {{results.length}} result(s).</span>

From docs:

You can also provide an optional alias expression which will then store the intermediate results of the repeater after the filters have been applied. Typically this is used to render a special message when a filter is active on the repeater, but the filtered result set is empty.

For example: item in items | filter:x as results will store the fragment of the repeated items as results, but only after the items have been processed through the filter.

How to display length of filtered ng-repeat data? · Issue #85 , {filtered .length}} I can display the length of filtered ng-repeat data in angular. But the variable is $vs_collection instead of filtered in vs-repeat. Angular js - Show message if ng-repeat is empty. Ask Question How to display length of filtered ng-repeat data. 1. Check for empty ng-repeat from controller. 0.

angularjs filter length, <button ng-click='items.splice(items.length-1,1)'>minus sth from the list</button>. 6. <hr>. 7. <ul>. 8. <li ng-repeat='item in filtered = (items | filter:filterExpr) track  There are two ways to count items in a filtered AngularJs list used in ngRepeat: to get filter count either in template or in controller. See how to do it.

limitTo, filter in module ng The length of the returned array or string. A new sub-array or substring of length limit or less if the input had less than limit elements. Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item. If you have an collection of objects,

How can we display length of filtered ng-repeat data ?, Let us assume that our list of people is in data variable and we have to filter people using query model, the following code will work for us:. How to display length of filtered ng-repeat data in AngularJS ? How to dynamically get the content width of a div using AngularJS ? Neon Text Display Using HTML & CSS

Comments
  • This is the simple answer that doesn't repeat filter execution.
  • @Ben: Yes. You can access it inside the controller using $scope.filtered.length.
  • That doesn't work for me. Logs undefined, likely because at the time of logging it, the variable isn't defined yet. So how would I ensure the code in the controller is run after the variable was defined and the filter was applied in the view?
  • @Ben: I guess this is going off-topic. I'd consider creating a custom filter e.g. jsfiddle, however, you could also watch it inside the controller: $scope.$watch('filtered', function() { console.log('filtered:', $scope.filtered); });
  • The 1.3+ version didn't work if I wanted to add limitTo : person in data | filter:query as filtered | limitTo:5. So I had to use the prior to 1.3 version: person in filtered = (data | filter: query) | limitTo: 5
  • Will this mean the filter is executed twice?
  • Yes, it is executed twice.
  • make sure you read @Wumms answer before you decide to use this one (and you won't)...
  • Nevermind, I see the answer didn't contain the part about the alias expression when you made your comment.
  • Yep, but I am glad you asked... I was not familiar with the alias expression. It is a good Q what should happen to Q's where the correct answer changes over time... should the person that provided the best answer (for when the Q was asked) loose his reputation because the correct answer he gave in the past is no longer the best? maybe there should be a difference between the accepted answer and the currently most accurate answer...