KnpPaginatorBundle icon indicating copy to clipboard operation
KnpPaginatorBundle copied to clipboard

Pagination - Remove extra query variable from page url

Open ldecoker opened this issue 2 years ago • 5 comments

|---------------- | ------ | Bundle version | 5.8.0 | Symfony version | 5.4 | PHP version | 7.4

Hello,

I got a list that I displayed using paginator. Under this list, I have added the pagination using the knp_pagination_render.

After deleting a record in a list, I used an ajax call to regenerate the list and so the pagination. To differenciate this ajax call from a normal one, I used a query variable "ajax". My issue is even if I removed the "ajax" variable from the query, when the pagination is rendered, it does include it, which break the page display when I want to go to another page.

is there a mechanism that would allow me to remove this extra query param from pagination?

thx!

ldecoker avatar Jan 25 '22 18:01 ldecoker

You should use appropriate Request method to detect ajax calls

garak avatar Jan 25 '22 19:01 garak

Hello! In the method to generate the list and the pagination, I am using at first thing:

if ($request->query->get('ajax' )) { $request->query->remove('ajax'); $template = '_list.html.twig'; } else { $template = 'list.html.twig'; }

So I would expect that the ajax variable should not be used by the pagination render method. And indeed if I dump the query params after that, it's not in anymore. I surely miss something.

Thx!

ldecoker avatar Jan 26 '22 09:01 ldecoker

Let me be more clear (sorry but yesterday I replied from my mobile): you should use isXmlHttpRequest method instead of relying on a query parameter.

garak avatar Jan 26 '22 10:01 garak

I am using fetch for my ajax calls and if I am not wrong, it does not send the needed information for isXMLHttpRequest.

ldecoker avatar Jan 26 '22 10:01 ldecoker

I am using fetch for my ajax calls and if I am not wrong, it does not send the needed information for isXMLHttpRequest.

You just need to use the proper header, something like this:

const req = new XMLHttpRequest();
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");

garak avatar Jan 26 '22 11:01 garak