首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony 2.1.4-knpPaginator在单击<th>元素时不会对数据进行排序

Symfony 2.1.4-knpPaginator在单击<th>元素时不会对数据进行排序
EN

Stack Overflow用户
提问于 2012-12-06 16:56:34
回答 1查看 1.9K关注 0票数 2

我的控制器正在使用原理并调用knp分页包的分页方法,代码如下

代码语言:javascript
复制
$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,
    ));

视图只是简单地表示分页数据

代码语言:javascript
复制
<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>

而路由是

代码语言:javascript
复制
index_entries:
   pattern: /index/
   defaults: { _controller: AcmeTaskBundle:Default:index }

我正在使用分页获取所有表数据,但无法在单击元素时对数据进行排序。分页器配置为

代码语言:javascript
复制
protected $defaultOptions = array(
    'pageParameterName' => 'page',
    'sortFieldParameterName' => 'sort',
    'sortDirectionParameterName' => 'direction',
    'distinct' => true
);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-29 15:16:20

我已经得到了答案。首先,我从分页器视图获取post数据,如下所示

代码语言:javascript
复制
$postData = $request->query->all();

然后在查询字符串中传递数据,如

代码语言:javascript
复制
$dql= 'select a from AcmeTaskBundle:Product a order by ORDER BY $postData[sort]    $postData[direction]';
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13740038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档