0
votes

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
1

1 Answers

1
votes

This happens because you are sending the form, I wrote you a piece of JS in your previous question which:

  • Gets the value of the selected option in eg. localhost/agence/web/app_dev.php/travels?sort=country&direction=desc

  • Redirect to the page you retrieved from the value.

I think it might also not work because KNPPaginator default template is a link, so instead of href = "KNP_LINK" need to value = "KNP_LINK". Modify the file

KnpPaginatorBundle \ Pagination \ sortable_option.html.twig