用KnpPaginator排序

时间:2017-09-14 07:46:27

标签: symfony sorting knppaginator

我在尝试使用KnpPaginator进行排序时遇到了问题。我遵循了许多链接,并在我的代码中尝试了很多迭代,它似乎无法正常工作。我错过了一些东西,却无法找到它 目前唯一可行的是桌面标题,我可以点击它,但没有任何事情发生,除了刷新页面并刷新我的分页1。 我正在研究symfony 2.3

这是我的捆绑配置

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:twitter_bootstrap_v3_pagination.html.twig'     # sliding pagination controls template
    sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template

这是我的控制器,我实际上用getRepository

设置了Knp
private function resultsAction(Request $request, User $user, $type, $archive)
{
    $em = $this->getDoctrine()->getManager();

    $query = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive);

    $paginator = $this->get('knp_paginator');
    $results = $paginator->paginate(
        $query,
        $request->query->getInt('page',1),
        $request->query->getInt('limit',50)
    );

    return array("results" => $results, "archive" => $archive);
}

public function offreAction(Request $request, User $user, $archive = false)
    {
        return $this->resultsAction($request, $user, Operation::OFFRE_COMMERCIALE, $archive);
    }

这是查询完成的我的回购:

public function getQueryByTypeAndPro($type, User $user, $archive)
    {
        return $this->createQueryBuilder("opn")
            ->andWhere("opn.type = :type")
            ->setParameter("type", $type)
            ->andWhere("opn.resellerId = :reseller")
            ->setParameter("reseller", $user->getId())
            ->andWhere("opn.archive = :archive")
            ->setParameter('archive', $archive)
            ->orderBy("opn.dateCreation", "DESC")
            ->getQuery()
        ;
    }

以下是我的观点,我尝试使用Knp

  <tr>
       <th>{{ knp_pagination_sortable(results, 'general.vehicule.ref'|trans, 'opn.type') }}</th>
       <th>{{ knp_pagination_sortable(results, 'general.vehicule.vehicule'|trans, 'opn.resellerId') }}</th>
      <th>{{ knp_pagination_sortable(results, 'notifications.client'|trans, 'opn.??') }}</th>
      <th>{{ knp_pagination_sortable(results, 'general.date'|trans, 'opn.??') }}</th>
      <th>{{ knp_pagination_sortable(results, 'commerce.achat.encours.etat'|trans, 'opn.??') }}</th>
  </tr>

在我的表格中,当我点击它时刷新页面但没有对其进行排序。我很难理解如何做到这一点?

我放置'opn.??'它的地方,因为我不知道在那个地方放什么,我似乎不理解查询

我希望最后一个日期项目是第一个,但能够使用Knp

对其进行排序

2 个答案:

答案 0 :(得分:1)

删除->orderBy("opn.dateCreation", "DESC")。 KnpPaginator有特殊的Listener和Walker,它可以根据请求使用正确的ORDER BY修改您的查询,然后为您执行分页。

配置here的可排序查看。 翻译看here

答案 1 :(得分:1)

好的,我成功完成了这项工作,这就是我所做的。

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: false                # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
    pagination: 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig'     # sliding pagination controls template
    sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template

回购

public function getQueryByTypeAndPro($type, User $user, $archive)
    {
        return $this->createQueryBuilder("opn")
            ->andWhere("opn.type = :type")
            ->setParameter("type", $type)
            ->andWhere("opn.resellerId = :reseller")
            ->setParameter("reseller", $user->getId())
            ->andWhere("opn.archive = :archive")
            ->setParameter('archive', $archive)
            ->orderBy("opn.dateCreation", "DESC")
            ->getQuery()
        ;
    }

控制器

private function resultsAction(Request $request, User $user, $type, $archive)
    {
        $em = $this->getDoctrine()->getManager();

        $paginator = $this->get('knp_paginator');

        $qb = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive);

        $results = $paginator->paginate(
            $qb,
            $request->query->get('page',1),
            $request->query->get('limit',50),
            [
                'defaultSortFieldName'      => 'opn.dateCreation',
                'defaultSortDirection' => 'desc'
            ]
        );

        return array("results" => $results, "archive" => $archive);
    }

和树枝

    <tr>
          <th{% if results.isSorted('opn.id') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.vehicule.ref'|trans, 'opn.id') }}</th>
          <th{% if results.isSorted('opn.vehiculeMarque') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.vehicule.vehicule'|trans, 'opn.vehiculeMarque') }}</th>
          {% if typeOffre is defined and typeOffre == 'devisWeb' %}<th>Financement</th>{% endif %}
          <th{% if results.isSorted('opn.clientNom') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'notifications.client'|trans, 'opn.clientNom') }}</th>
          <th{% if results.isSorted('opn.dateCreation') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.date'|trans, 'opn.dateCreation') }}</th>
          <th>{{ 'commerce.achat.encours.etat'|trans }}</th>
         <th class="sorting_disabled">{{ 'commerce.achat.action'|trans }}</th>
        <th class="sorting_disabled">{{ 'commerce.vente.operation.basculer'|trans }}</th>
 </tr>

所以有效