15

I am creating student report list with Angularjs ng-repeat. My issue if how i can dynamically appending numbering like ordered-list to the generated list in view. I want to achieve something like this

 # | Name of student | Student ID
 _________________________________
 1 | Samuel Addo     | 346578
 2 | GRace Asumani   | 965433
 3 | Zein Akill      | 123455
 4 | David Addoteye  | 678543

The '#' column should be auto generate when rendering the model in view through ng-repeat. Honestly I don't know where to start cos i don't know how to do it. I will be glad if anyone can help me or point me to the right source. Thank you.

2
  • What should happen if you clicked the '#' header, and had it reverse-sort? Or is the desired behavior that '#' column is always incrementing Mar 17, 2015 at 5:27
  • it is only for numbering purpose, not intend to sort by it. thank you Mar 17, 2015 at 5:32

2 Answers 2

62

Inside ng-repeat, you can use:

{{$index +1}}

So for example:

<tr ng-repeat="student in students">
  <td>#{{$index + 1}}</td>
  <td>{{student.name}}</td>
</tr>

$index is a variable provided by the ng-repeat directive which gives you the current index. Here I added 1 to it so that the numbers start from 1 instead of 0. ng-repeat documentation

4
  • 1
    This answer is enough for the solution. It's good (although obvious), to let user know that {{$index}} counts the loop indexes just like any loops in Javascript. We can do arithmetic with as well.
    – Bikas
    Mar 17, 2015 at 5:26
  • Waow, that was fast, thanks man. Please can you also help me on how to recalculate the model length when it is filtered. for instance if i have 10 records and i filter it by name to 4, how i can update the model length to 4? thank you again Mar 17, 2015 at 5:28
  • 1
    Sorry, wrote the answer on my phone, I'll edit it now to a more complete example. Also, good to hear that helped. As for the other problem, I'm not quite sure. I'll think about it.
    – juunas
    Mar 17, 2015 at 5:36
  • Thanks. This is what I'm looking for. :D Aug 7, 2017 at 3:24
4

Yes you can use {{$index}} to print index position for serial no inside ng-repeat.

Various other variables also available for calculation and check for first, middle, last, odd, even using $first, $middle, $last, $odd and $even respectively.

  • $index: iterator offset of the repeated element (0..length-1)
  • $first: true if the repeated element is first in the iterator.
  • $middle: true if the repeated element is between the first and last in the iterator.
  • $last: true if the repeated element is last in the iterator.
  • $even: true if the iterator position $index is even (otherwise false).
  • $odd: true if the iterator position $index is odd (otherwise false).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.