4
votes

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?

2

2 Answers

9
votes

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 }}
2
votes

Use this code:

{{ pagination.getPaginationData.firstItemNumber + loop.index - 1 }}