I'd like to and retrieve sortable options list with KnpPaginatorBundle but I failed. This is the form:
<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>
And this is the view in KnpPaginatorBundle\Pagination\sortable_option.html.twig
<option {% for attr, value in options %} {{ attr }}="{{ value }}"{% endfor %}>{{ title }}</option>
and this is the action:
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
));
}
I'd like to know how to create the options correctly in the form with sort=country and direction=asc and the second option with sort=country and direction=desc , also I'd like to retrieve those parameters in the controller after POST and correct mistakes in the Action (near explode).
Right now when I select the first choice for example I get this url:
http://localhost/agence/web/app_dev.php/travels?option=Country+A-Z