简体   繁体   English

在Symfony2中使用Pagerfanta进行分页的正确方法是什么

[英]What is correct way to use Pagerfanta for pagination in Symfony2

I have read that Pagerfanta is the pagination plugin of choice for Symfony2, but I am having some trouble getting it to work correctly. 我已经读过,Pagerfanta是Symfony2的首选分页插件,但我在使其正常工作时遇到了一些麻烦。 I have downloaded the code for pagerfanta and PagerfantaBundle and installed them as described in the WhiteOctober Github readme file and in this tutorial . 我已经下载了pagerfanta和PagerfantaBundle的代码,并按照WhiteOctober Github自述文件本教程中的描述安装它们。 I'm pretty sure the code in my controller is okay, but there is something wrong with my template file. 我很确定我的控制器中的代码是可以的,但我的模板文件有问题。

My controller code: 我的控制器代码:

public function indexAction($page =  null)
{
    ...
    $query = $em->createQuery('SELECT something FROM some_table');
    $adapter = new DoctrineORMAdapter($query);
    $pager =  new Pagerfanta($adapter);
    $pager->setMaxPerPage(10);
    if (!$page)    $page = 1;
    try  {
        $pager->setCurrentPage($page);
    }
    catch(NotValidCurrentPageException $e) {
      throw new NotFoundHttpException('Illegal page');
    }
    return $this->render('MyBundle:MyEntity:index.html.twig', array(
        'pager' => $pager,
    ));
}

In my template: 在我的模板中:

...
<table>
  {% for object in pager.currentPageResults %}
    <tr>
      <td>{{ object.attribute1 }}</td>
      <td>{{ object.attribute2 }}</td>
      <td>{{ object.attribute3 }}</td>
    </tr>
  {% endfor %}
</table>

{% if pager.haveToPaginate %}
  {{ pagerfanta(pager) }}
{% endif %}

When I go to the page, I get a list of the first 10 objects. 当我转到页面时,我得到了前10个对象的列表。 However,the rendering of the pager is just PreviousNext as one word (not links and no pages 1, 2, 3, etc). 但是,寻呼机的呈现只是PreviousNext作为一个单词(不是链接,没有页面1,2,3等)。

If I add ?page=2 to the URL, it doesn't display the next 10 items in the list. 如果我将?page=2添加到URL,它不会显示列表中的下10个项目。

I didn't change the route from what is was before I added the pagination code because the documentation says: 在添加分页代码之前,我没有更改路径,因为文档说:

The routes are generated automatically for the current route using the variable "page" to propagate the page number. 使用变量“page”自动为当前路由生成路由以传播页码。

... but I wonder if incorrect routing is part of the problem. ...但我想知道错误的路由是否是问题的一部分。 Assuming this is the case, I'm not sure how to proceed with this. 假设是这种情况,我不知道如何继续这样做。

I have been unable to find any instructions other the sources I have linked to above, so it would be great if someone who has implemented this successfully could share the details of how they did it. 我一直无法找到上面链接到的其他来源的任何说明,所以如果成功实现此功能的人可以分享他们如何做到这一点的详细信息将会很棒。

I think your application doesn't know how to parse ?page=2 to the controller action. 我认为您的应用程序不知道如何解析?page=2到控制器操作。

Include a {page} in your route (as @AdrienBrault replied), which you wish to have pagination. 在您的路线中添加{page}(如@AdrienBrault所述),您希望对其进行分页。

like this: 像这样:

my_index:
  pattern:  /{page}
  defaults: { _controller: MyTestBundle:Object:index, page: 1 }
  requirements:
    _method:  GET
    page: \d+

PagerFantaBundle will (I guess) try to guess the correct route. PagerFantaBundle(我猜)会尝试猜测正确的路线。

Worked for me. 为我工作。

我不得不在我的路由中使用'currenPage'而不是'page',我找到了一种方法:

{{ pagerfanta(pager, 'my_pager_view', {'pageParameter':'[currentPage]'}) }}

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

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