1

我在尝试使用 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 对其进行排序

4

2 回答 2

2

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

Config sortable 看这里。翻译看这里

于 2017-09-14T08:04:47.260 回答
2

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

配置.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>

这样就可以了

于 2017-09-14T14:05:44.247 回答