KnpPaginatorBundle
KnpPaginatorBundle copied to clipboard
Pagination - Remove extra query variable from page url
|---------------- | ------ | 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!
You should use appropriate Request method to detect ajax calls
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!
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.
I am using fetch for my ajax calls and if I am not wrong, it does not send the needed information for isXMLHttpRequest.
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");