1

I'm using angularjs in a site and there I have a search input that filters a list on a view. This list is displayed with a ng-repeat that has a filter from the search input:

The search input:

<input type="text" placeholder="Device Search" class="form-control hasclear" 
  ng-model="searchText"/>

And here the ng-repeat:

<tr ng-click="openModal(device['device_id'], device)" 
  ng-repeat="device in devices | filter:searchText | orderBy:predicate:reverse">

As you see the filter in the ng-repeat has the filter that uses the searchText variable from the ng-model. I would like to know if it's possible to know how many objects were found when the user enters a text in the search input (How many devices the ng-repeat is displaying being filtered). Like: 0 devices were found, 3 devices were found...

Is there a way to display this information by the way I built this search?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Joabe da Luz
  • 1,030
  • 2
  • 18
  • 32

4 Answers4

2

EDIT:

Based on Dave's response which is correct, you must assign a variable to the filtered result as so

<tr ng-click="openModal(device['device_id'], device)" 
ng-repeat="device in filtered_devices = (devices | filter:searchText | orderBy:predicate:reverse)">

Then, below your tr tag you can type:

<div>{{filtered_devices.length + ' devices were found'}}</div>

Plunkr example here

thank_you
  • 11,001
  • 19
  • 101
  • 185
2

If you need it outside of your <tr> tag, I would do it like this:

<tr ng-click="openModal(device['device_id'], device)" 
ng-repeat="device in filteredResults = (devices | filter:searchText | orderBy:predicate:reverse)">

{{filteredResults.length}} devices were found
dave
  • 62,300
  • 5
  • 72
  • 93
1

with a filter like filter:x as result it stores the resultant collection on the variable result.

So you can do:

<tr ng-click="openModal(device['device_id'], device)" ng-repeat="device in devices | filter:searchText as filteredCollection | orderBy:predicate:reverse">
  <td><span>Filtered collection size: {{filteredCollection.length}}</span><td>
</tr>
cefigueiredo
  • 738
  • 3
  • 12
0

I am sure there is a possibility with the properties:

  • $last
  • $index

something like:

<span ng-if="$last" > {{$index}} devices found </span>

documentation: https://docs.angularjs.org/api/ng/directive/ngRepeat

Joniras
  • 1,258
  • 10
  • 32