我的控制器正在使用学说并调用 knp pagination bundle 的 paginate 方法,代码如下
$em = $this->get('doctrine.orm.entity_manager');
$query = $em->createQueryBuilder()->add('select', 'a')
->add('from', 'Acme\TaskBundle\Entity\Product a')->getQuery()->getResult();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
5/*limit per page*/
);
return $this->render('AcmeTaskBundle:Default:index.html.twig', array(
'pagination' => $pagination,
));
而视图只是代表分页数据
<table>
<tr>
{# sorting of properties based on query components #}
<th>{{ pagination.sortable('Id', 'a.id')|raw }}</th>
<th>{{ pagination.sortable('Name', 'a.name')|raw }}</th>
<th>{{ pagination.sortable('Price', 'a.price')|raw }}</th>
<th>{{ pagination.sortable('Description', 'a.description')|raw }}</th>
</tr>
{# table body #}
{% for Product in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ Product.id }}</td>
<td>{{ Product.name }}</td>
<td>{{ Product.price }}</td>
<td>{{ Product.description }}</td>
</tr>
{% endfor %}
</table>
{# display navigation #}
<div class="navigation">
{{ pagination.render()|raw }}
</div>
和路由是
index_entries:
pattern: /index/
defaults: { _controller: AcmeTaskBundle:Default:index }
我通过分页获取所有表格数据,但无法在单击元素时对数据进行排序。分页器配置是
protected $defaultOptions = array(
'pageParameterName' => 'page',
'sortFieldParameterName' => 'sort',
'sortDirectionParameterName' => 'direction',
'distinct' => true
);