我很确定必须有一个干净,漂亮的方法来做到这一点。 我需要为用户提供查看页面中表格的所有行的选项(这里的分页也可能很棒),然后允许他过滤那些在某些特定字段中具有某些价值的行。允许重新订购此结果也很棒。
因为我不知道从哪个过滤器或排序将适用我只需将所有数据集提供给我的twig模板,然后打印所有这些。直到这里一切都好。 但这是我不知道如何继续......
假设你有:
ControllerDefault.php
...default header....
public function listAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('ACMEAppBundle:Item')->findAll();
return $this->render('ACMEAppBundle:Default:list.html.twig', array('entities'=>$entities));
}
list.html.twig
{% extends '::base.html.twig' %}
{% block content %}
<h3>List of items</h3>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>details</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.id }}</td>
<td>{{ entity.name }}</td>
<td>{{ entity.details }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{%endblock%}
您是否会使用带有按钮的文本进行操作,然后通过将某种变量发送回控制器来尝试生成新的URL?或者可能为不同的动作生成网址?
你会不会在这个页面上看到AJAX ......如果是这样的话?怎么会是最基本的实现?
任何建议都会受到高度赞赏,因为这是真正的工作,我的终结是提前两天(我是如何结束这样做的另一个故事xD)
感谢!!!
答案 0 :(得分:4)
你需要这个:KnpPaginatorBundle
1)将它添加到您的composer.json文件(/composer.json):
{
"require": {
"knplabs/knp-paginator-bundle": "~2.4"
}
}
2)在您项目的根目录中键入您的控制台:
composer update knplabs/knp-paginator-bundle
如果你没有作曲家: https://getcomposer.org/download/
3)将此添加到您的config.yml(/app/config/config.yml):
knp_paginator:
page_range: 5 # default page range used in pagination control
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
pagination: KnpPaginatorBundle:Pagination:sliding.html.twig # sliding pagination controls template
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
4)在AppKernel.php(/app/AppKernel.php)中注册包:
public function registerBundles()
{
return array(
// ...
new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
// ...
);
}
5)现在替换listAction(ControllerDefault.php):
public function listAction()
{
$em = $this->getDoctrine()->getManager();
$dql = "SELECT i FROM ACMEAppBundle:Item i";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$entities = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
10/*limit per page*/
);
return $this->render('ACMEAppBundle:Default:list.html.twig', array('entities'=>$entities));
}
6)最后你必须改变观点(list.html.twig):
{% extends '::base.html.twig' %}
{% block content %}
<h3>List of items ({{ entities.getTotalItemCount }})</h3>
<table>
<thead>
<tr> {# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(entities , 'Id', 'i.id') }}</th>
<th>{{ knp_pagination_sortable(entities , 'Name', 'i.name') }}</th>
<th>{{ knp_pagination_sortable(entities , 'Details ', 'i.details ') }}</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.id }}</td>
<td>{{ entity.name }}</td>
<td>{{ entity.details }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{# display navigation #}
<div class="pagination">
{{ knp_pagination_render(entities) }}
</div>
{% endblock %}