Lubudubu1010
Lubudubu1010

Reputation: 199

Angular JS index of selected TD

<tr ng-repeat="car in cars | filter:search:strict | orderBy: 'Type'">
    <td>{{car.Type}}</td>
    <td>{{car.Name}}</td>
    <td>{{car.Year}}</td>
</tr>

Can I get a car's index: a number which represents a place in cars array? Currently, it is not in right order, because of orderBy filter.

Upvotes: 1

Views: 792

Answers (2)

rob
rob

Reputation: 18513

You could just call indexOf() with your car object to get its original index

<tr ng-repeat="car in cars | filter:search:strict | orderBy: 'Type'" ng-click="someFunc(cars.indexOf(car))">
    <td>{{car.Type}}</td>
    <td>{{car.Name}}</td>
    <td>{{car.Year}}</td>
</tr>

Upvotes: 0

Mick Cullen
Mick Cullen

Reputation: 9454

Do the sorting in a controller. Then place the sorted items into a cars array. Then do

<tr ng-repeat="car in cars track by $index">
  <td>{{$index}}</td>
  <td>{{car.Type}}</td>
  <td>{{car.Name}}</td>
  <td>{{car.Year}}</td>
</tr>

Upvotes: 1

Related Questions