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
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.
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.
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.
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:
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>
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')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With