简体   繁体   English

如何获取当前项目索引 Knp 分页器

[英]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:我正在尝试获取当前显示在 knp 分页器包中的项目的当前索引,atm 我有这样的东西:

{% 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.但是,即使我在第 2 页,这只显示例如 1-10。它应该在第 3 页上显示项目 11-20,在第 3 页上显示 21-30,...但它每次都会重置。

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. 我只是在寻找一个更清晰的解决方案,并发现getPaginationData方法运行良好。 To display the range, you could do something like: 要显示范围,您可以执行以下操作:

{{ pagination.getPaginationData.firstItemNumber }} - {{ pagination.getPaginationData.lastItemNumber }}

使用此代码:

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

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

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM