gpt4 book ai didi

symfony - 使用 KnpPaginator 排序

转载 作者:行者123 更新时间:2023-12-02 00:41:45 25 4
gpt4 key购买 nike

我在尝试使用 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

这是我的 Controller ,我在其中实际使用 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);
}

这是我完成查询的 repo 协议(protocol):

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 对其进行排序

最佳答案

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

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

repo

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()
;
}

Controller

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);
}

和 Twig

    <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>

这样就可以了

关于symfony - 使用 KnpPaginator 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46213350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com