0

在我的演示应用程序中,使用 Pagerfanta 从数据库中仅读取几行。

public function findAllProductsByCategory($category, int $page = 1): Pagerfanta
{
    // DQL = Always select full classes from classes
    $query = $this->getEntityManager()
        ->createQuery('
             SELECT   p
             FROM     App\Entity\Product p
             WHERE    p.category = :category
        ')
        ->setParameter('category', $category);

    return $this->createPaginator($query, $page);
}

后来在 Twig 中,我循环了预选的结果。一切都很好 ...

    {% if products.haveToPaginate %}
        <div class="navigation text-center">
            {{ pagerfanta(products, 'default', {routeName: 'category_show_paginated', 'routeParams' : { 'id': category.id}}) }}
        </div>
    {% endif %}
    <table border="1" cellpadding="5">
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Products</th>
            <th>Description</th>
        </tr>
        {% for product in products %}
        <tr>
            <td>{{ loop.index }}</td>
            <td><a href="{{ path('products_show', {id: product.id}) }}">{{ product.name }}</a></td>
            <td>{{ product.description }}</td>
        </tr>
        {% endfor %}
    </table>

但在每一页上,“loop.index”都是相同的 1,2,3,4 ...我想用 pagerfanta 显示结果数 41,42,43 ...;)

有什么特殊功能吗?

谢谢

4

1 回答 1

1

Pagerfanta一段时间没有使用过,但是您想要的应该可以通过以下方式实现:

{{ pagerfanta.currentPageOffsetStart() + loop.index }}

示例测试用例:https ://hotexamples.com/examples/pagerfanta/Pagerfanta/getCurrentPageOffsetStart/php-pagerfanta-getcurrentpageoffsetstart-method-examples.html

于 2018-04-14T15:44:33.817 回答