4

I am trying to get the current index of the item that is current displaying in knp paginator bundle, atm i have something like this:

{% for index, post in posts %}

    {{ index + 1 }}

{% endfor %}

However this only displays for example 1 - 10, even when i am on page 2. It is supposed to show items 11-20 on page, 21-30 on page 3,... but it gets reset every time.

I solved it by using:

{{ pagination.getItemNumberPerPage * (pagination.getCurrentPageNumber - 1) + index + 1 }}

but this is a very messy solution which i don't want to use.

Are there any alternatives for this?

Maxim
  • 3,836
  • 6
  • 42
  • 64

3 Answers3

9

I was just looking for a cleaner solution to this also, and found that the getPaginationData method works pretty well. To display the range, you could do something like:

{{ pagination.getPaginationData.firstItemNumber }} - {{ pagination.getPaginationData.lastItemNumber }}
sarahg
  • 161
  • 3
  • 5
2

Use this code:

{{ pagination.getPaginationData.firstItemNumber + loop.index - 1 }}
sergio
  • 297
  • 3
  • 17
-1

{{ pagination.getPaginationData.firstItemNumber + loop.index0 }}