Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Pagination direction in twig for KNP paginator?

I am using knp paginator and it works well but when I want to use its sorting feature I have problem to get the sort direction in twig.

the following code is indicate how to get the sorted table header but not taking about how to get sorted table header direction.

{# 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 KnpPaginator documentation on following link: https://github.com/KnpLabs/KnpPaginatorBundle

like image 216
Ehsan Avatar asked Nov 17 '13 07:11

Ehsan


People also ask

What is KNP paginator in Angular 2?

Knp Paginator first was introduced as a Bundle for Sf2 projects and was. using Zend as the main dependency to handle pagination. Recently we have split. it into a Pager Component. without any hard dependencies except. Symfony EventDispatcher Component.

Can I add pagination to my twig implementation?

For the moment, we are only concerning ourselves with adding Pagination into the Twig implementation. However, what you are about to learn will also apply - in the vast majority - to every other implementation. This is because all of this happens on the server side.

How do I use a paginator?

And once we have the paginator, we can use it really easily by calling paginate, and passing in the 'thing' to paginate, and optionally the page we want, the amount of results per page, and some options - which we won't need to use. It's important to that we pass in a Query to our paginator, rather than a result.

Is KNP paginator compatible with Symfony 3?

One thing to note, the current docs for KNP Paginator (at the time of writing) say that this is a paginator for Symfony2. It is, but it's also compatible with Symfony 3. Installing the bundle is largely copy / paste from the documentation: Follow the instructions to add the PaginatorBundle into AppKernel.php:


2 Answers

You should be able to just use {{ pagination.getDirection() }} in your twig template to find the current sort direction (if any) then set up your classes based on that.

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

But... as of this post, KNP has not yet merged this fix: https://github.com/sroze/KnpPaginatorBundle/commit/3105a38714c6f89c590e49e9c50475f7a777009d

When there is no direction parameter set, the current Paginator bundle throws an error.

So, until the above fix is merged, you can still get the direction with a bit more verbosity:

{% 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>
like image 110
nateevans Avatar answered Sep 23 '22 19:09

nateevans


When you call {{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}, bundle automatically generates link with a class holding an information about 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 style it with the arrow. If you, for some reason, need to get a sort direction inside a controller, just read it from request $request->query->get('direction').

like image 45
Aljana Polanc Avatar answered Sep 23 '22 19:09

Aljana Polanc