简体   繁体   English

如何获得结果数? -Pagerfanta和Twig(在Symfony中)

[英]How to get the number of result? - Pagerfanta and Twig (in Symfony)

In my Demo application, use Pagerfanta to read only a few rows from Database. 在我的演示应用程序中,使用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);
}

Later in Twig, I loop the preselected results. 稍后在Twig中,我循环显示预选的结果。 Everything is fine ... 一切顺利 ...

    {% 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>

but on each page, "loop.index" is the same 1,2,3,4 ... I would like to show the number of results 41,42,43 ... with pagerfanta ;) 但在每一页上,“ loop.index”是相同的1,2,3,4 ...我想用pagerfanta显示结果数41,42,43 ...)

Is there a special function? 有特殊功能吗?

Thank you 谢谢

Haven't worked with Pagerfanta for a while, but what you want should be doable with something like this: 一段时间没有与Pagerfanta合作,但是您想要的应该可以通过以下方式实现:

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

Example test case: https://hotexamples.com/examples/pagerfanta/Pagerfanta/getCurrentPageOffsetStart/php-pagerfanta-getcurrentpageoffsetstart-method-examples.html 测试案例示例: https : //hotexamples.com/examples/pagerfanta/Pagerfanta/getCurrentPageOffsetStart/php-pagerfanta-getcurrentpageoffsetstart-method-examples.html

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

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