Inoubli
Inoubli

Reputation: 374

Where to add 'track by $index' when using Filter and Pagination in the same ng-repeat

I have my ng-repeat and filter ,then I added bootstrap-ui pagination and everything working fine

To add fields Edit, I needed to use 'track by $index' but I cant find the correct way to add it to my ng-repeat, This is my HTML view :

<tr data-ng-repeat="target in targets | filter:search | startFrom:(currentPage - 1) * pageSize  | limitTo: pageSize "> 
  <td><input type="text" ng-model-options="{ updateOn: 'blur' }" ng-change="updateTarget(targets[$index])" ng-model="targets[$index].YEAR"></td>
  <td><input type="text" ng-model-options="{ updateOn: 'blur' }" ng-change="updateTarget(targets[$index])" ng-model="targets[$index].MONTH"></td>
  <td><input type="text" ng-model-options="{ updateOn: 'blur' }" ng-change="updateTarget(targets[$index])" ng-model="targets[$index].TV"></td>
</tr>

Here is my table screen shot

My PageSize Limit is set to 5, so my index restart from 1 to 5 in every page cause I didn't added track by $index in my ng-repeat So if I edit row number 2, it will edit every row number 2 in every page,

Any idea for how to add 'track by $index' when using Filter and Pagination in the same ng-repeat

Upvotes: 1

Views: 1035

Answers (1)

Rob J
Rob J

Reputation: 6629

According to the Angular documentation here:

Note: track by must always be the last expression:

<div ng-repeat="model in collection | orderBy: 'id' as filtered_result track by model.id">
    {{model.name}}
</div>

Upvotes: 0

Related Questions