How to get pagination direction in branch for paginator KNP?

I use paginator knp and it works well, but when I want to use its sort function, I have a problem to get the sort direction in the branch.

The following code indicates how to get the header of the sorted table, but not accept the sort order of the table header.

{# total items count #} <div class="count"> {{ pagination.getTotalItemCount }} </div> <table> <tr> {# sorting of properties based on query components #} <th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th> <th{% if pagination.isSorted('a.Title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'a.title') }}</th> </tr> {# table body #} {% for article in pagination %} <tr {% if loop.index is odd %}class="color"{% endif %}> <td>{{ article.id }}</td> <td>{{ article.title }}</td> </tr> {% endfor %} </table> {# display navigation #} <div class="navigation"> {{ knp_pagination_render(pagination) }} </div> 

I get this code from the KnpPaginator documentation at the following link: https://github.com/KnpLabs/KnpPaginatorBundle

+6
source share
2 answers

You can simply use {{ pagination.getDirection() }} in your branch template to find the current sort direction (if any), and then customize your classes based on this.

 {% set direction = pagination.getDirection() %} <th{% if pagination.isSorted('p.id') %} class="sorted {{ direction }}"{% endif %}> {{ knp_pagination_sortable(pagination, 'Id', 'p.id') }} </th> 

But ... KNP has not yet combined this fix from this post: https://github.com/sroze/KnpPaginatorBundle/commit/3105a38714c6f89c590e49e9c50475f7a777009d

If there is no direction parameter specified, the current Paginator package throws an error.

So, until the patch is merged, you can still get the direction with a bit more detail:

 {% set directionParam = pagination.getPaginatorOption('sortDirectionParameterName') %} {% set params = pagination.getParams() %} {% set direction = params[directionParam] is defined ? params[directionParam] : null %} <th{% if pagination.isSorted('p.id') %} class="sorted {{ direction }}"{% endif %}> {{ knp_pagination_sortable(pagination, 'Id', 'p.id') }} </th> 
+9
source

When you call {{ knp_pagination_sortable(pagination, 'Id', 'a.id') }} , the package automatically generates a link with a class containing information about the sort direction, which looks something like this: <a translationcount="" class="asc" href="?sort=a.id&direction=desc&page=1" title="Id">Id</a> So just put this class in your css file and create it with an arrow. If for some reason you need to get the sort direction inside the controller, just read it with the request $request->query->get('direction') .

+4
source

Source: https://habr.com/ru/post/958244/


All Articles