Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current item index Knp paginator

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?

like image 566
Maxim Avatar asked Dec 20 '22 13:12

Maxim


2 Answers

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 }}
like image 153
sarahg Avatar answered Jan 05 '23 10:01

sarahg


Use this code:

{{ pagination.getPaginationData.firstItemNumber + loop.index - 1 }}
like image 38
sergio Avatar answered Jan 05 '23 10:01

sergio