使用 KnpPaginator 排序
sorting with KnpPaginator
我在尝试使用 KnpPaginator 进行排序时遇到问题。我点击了很多链接并在我的代码中尝试了很多迭代,它似乎无法正常工作。我丢失了一些东西,但找不到它
目前唯一有效的是 table headers 我可以点击它但除了刷新页面并让我回到分页 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>
而在我的 table 中,当我点击它时会刷新页面但不会对其进行排序。我很难理解如何让它发挥作用?
我放的地方 'opn.??'
因为我不知道在那个特定的地方放什么,我似乎不明白查询
我希望日期最后的项目排在第一位,但可以使用 Knp 对其进行排序
删除 ->orderBy("opn.dateCreation", "DESC")
。 KnpPaginator 有特殊的 Listener 和 Walker,可以根据请求使用正确的 ORDER BY 修改您的查询,然后为您执行分页。
好的,我成功完成了这项工作,这就是我所做的。
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>
这样就可以了
我在尝试使用 KnpPaginator 进行排序时遇到问题。我点击了很多链接并在我的代码中尝试了很多迭代,它似乎无法正常工作。我丢失了一些东西,但找不到它 目前唯一有效的是 table headers 我可以点击它但除了刷新页面并让我回到分页 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
设置了 Knpprivate 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>
而在我的 table 中,当我点击它时会刷新页面但不会对其进行排序。我很难理解如何让它发挥作用?
我放的地方 'opn.??'
因为我不知道在那个特定的地方放什么,我似乎不明白查询
我希望日期最后的项目排在第一位,但可以使用 Knp 对其进行排序
删除 ->orderBy("opn.dateCreation", "DESC")
。 KnpPaginator 有特殊的 Listener 和 Walker,可以根据请求使用正确的 ORDER BY 修改您的查询,然后为您执行分页。
好的,我成功完成了这项工作,这就是我所做的。
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>
这样就可以了