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?
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 }}
Use this code:
{{ pagination.getPaginationData.firstItemNumber + loop.index - 1 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With