How To append parameters to KNP paginator url

5.2k Views Asked by At

I am using the KNP Paginator bundle for pagination. Does anyone know how to append parameters to the generated url?

Here is my set up:

 {{ knp_pagination_sortable(supplierProducts, 'Product Sku', 'p.name') }}

I want to add &section=supplier to the end of the URL, I just have no clue how to do it. I looked through the docs but did not find any info.

Please help if you can.

Thanks.

4

There are 4 best solutions below

0
On

As of 2020, and KnpPaginatorBundle v5.3, the solution proposed by @likeitlikeit doesn't work because the setParam method doesn't exist any more.

But you can append parameters to the sort and pagination links directly in the knp helpers in twig:

{# 4th parameter for sortable helper #}
{{ knp_pagination_sortable(results, column.title, column.alias, {}, params) }}

{# 3rd parameter for pagination helper #}
{{ knp_pagination_render(results, '', params) }}

For example, if you want to include the query parameters in the sort and pagination links, you can do:

{# Sort - pass all query parameters except sort column and direction #}
{% set params=app.request.query.all | filter((v, k) => (k != 'direction' and k != 'sort'))%}
{% for column in ... %}
    {{ knp_pagination_sortable(results, column.title, column.alias, {}, params) }}
{% endfor %}

{# Pagination #}
{{ knp_pagination_render(results, '', app.request.query.all) }}
0
On

To add parameters in the url, I proceeded like this: in Front:

 {{ knp_pagination_render(clients, ('annuaire/my_pagination.html.twig'), {"type": type ? type : '' ,"city": city ? city : ''}) }}
1
On

According to the KnpPaginator documentation, you can append query parameters as follows:

$paginator = $this->get('knp_paginator');
...
$pagination->setParam('section', 'supplier');
0
On

You could extend the kn_pagination_sortable template. When you run "knp_pagination_sortable" behind the scenes, it will basically generate an HTML according to your specifications. However, you can extend that. Instead of using the bundle's generated HTML for that element, you can write your own template for that pagination_sortable. This is a snippet from the project I'm working on. This is at my pagination_sortable.html.twig:

<a id="table_sorteable_{{ options['title']|lower }}" {% for attr, value in options %} {{     attr }}="{{ value }}"{% endfor %}>
    {{ title }}
    <b class="caret {{ options['class'] }}"></b>
</a>

Get it? You can have a template like that and change it according to your needs.

You can find more information on the link below.

Overriding default pagination template