我想用 KnpPaginatorBundle 检索可排序的选项列表,但我失败了。这是表格:
<form action="" method="get" class="form_sort" id="myForm">
<span class="manage_title">Sort by:</span>
<select class="select_styled white_select" id="sort_list" name="option" onChange="sendForm();">
<option value="">-------</option>
{{ knp_pagination_sortable(pagination, 'Country A-Z', 'country', {'sort': 'country', 'direction': 'asc'}) }}
{{ knp_pagination_sortable(pagination, 'Country Z-Z', 'country', {'sort': 'country', 'direction': 'desc'}) }}
</select>
</form>
这是 KnpPaginatorBundle\Pagination\sortable_option.html.twig 中的视图
<option {% for attr, value in options %} {{ attr }}="{{ value }}"{% endfor %}>{{ title }}</option>
这是行动:
public function listAction($page, Request $request)
{
$em = $this->getDoctrine()->getManager();
$paginator = $this->get('knp_paginator');
if ($request->getMethod() == 'POST')
{
$option = $request->query->get('option'); //get query option
list($directiuon, $sort) = explode(":", $option);
$listTravels = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend($sort, $direction);
$pagination = $paginator->paginate(
$listTravels,
$this->get('request')->query->get('page', $page)/*page number*/,
5/*limit per page*/
);
return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
'pagination' => $pagination
));
}
$listTravels = $em->getRepository('ProjectTravelBundle:Travel')->findAll();
$pagination = $paginator->paginate(
$listTravels,
$this->get('request')->query->get('page', $page)/*page number*/,
5/*limit per page*/
);
return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
'pagination' => $pagination
));
}
我想知道如何以sort=country 和 direction=asc的形式正确创建选项,以及sort=country 和 direction=desc的第二个选项,我也想在POST之后在控制器中检索这些参数并纠正动作中的错误(接近爆炸)。
现在,当我选择第一个选项时,例如我得到这个 url:
http://localhost/agence/web/app_dev.php/travels?option=Country+A-Z