How do I make DataTables redraw a server-side table and maintain the current search and page length?
How do I make DataTables redraw a server-side table and maintain the current search and page length?
Hiya!
I'm currently using datatables to display large resultsets from an API. We perform actions on this resultset, causing some rows to be removed, after which I want to redraw the table -- basically, rows are selected, and a PUT request is performed updating a relationship in the API and causing them to no longer be part of the result set from the API.
I have previously solved this by doing:
$("#load-data-table table").DataTable().ajax.url($("#load-data-table table").DataTable().ajax.url()+"&length="+$("select[name='ajax-table_length']").val())
$("#load-data-table table").DataTable().ajax.reload()
If a user has searched for anything and a subset of the resultset is displayed, which correctly refreshes the table (but does not send the correct parameters to the API again, it only reloads from the same URL without passing all the column[0][... parameters). It also breaks a lot of other stuff, like searching afterwards, and it doesn't maintain the search value either.
To do this, I've instead tried to replace this code with
$("#load-data-table table").DataTable().search($("input[type=search]").val()).draw();
And ... This works fine if I try it in a console. But as soon as I've removed any rows from the table, it breaks: Basically, it stops doing anything but just reloads the default URL without maintaining search parameters, nor sending the datatable column parameters to the server.
The main problem I seem to be having is that the column, search and length parameters are not sent again when the ajax is reloaded, which means the wrong data is returned and the displayed table is not correct, and it happens both when using the .search function to just re-search for the value, and when using the ajax.reload method..
How can I make it reload ajax (WITH all the same columns, length and search parameters) and then redraw the table?
Best regards,
Amnesthesia
Answers
The
draw()
takes params that can prevent the table from doing that, so trytable.search(table.search()).draw(false)
i believe. Im on my cell now, ill double check laterThank you jLinux, but .. I get the same result from this though. An AJAX query is sent without the column[0][..., length and search parameters and the default resultset without any paging is returned (since there's no length parameter being sent) :(
If you are using server-side processing (
serverSide
), just usedraw()
-reload.ajax()
isn't particularly useful with server-side processing.If that doesn't work for you, we'd need a link to a test case showing the issue.
Allan
It's an internal system working with an internal API so I can't really provide a test case I'm afraid :( I really wish I could..
Basically, I'll try to explain as best as I can what's happening:
Each row is then clickable, and will be added to
selected_rows
when selected, and when Apply is clicked it will trigger a PUT-request for each row which causes a query to the API where the ID from this row, the elements ID, is assigned to a relationship in the API and thus will not be shown in the table. The rows are removed after the PUT request is sent, and the table is redrawn after the last PUT requests has finished and theselected_rows
array is empty again.This code is as follows:
The problem is that whenever I call .draw() it does redraw the table but it does not send the parameters with which columns are searchable, orderable, etc, what search parameter was used, or the length value. It makes a new API call with the URL set up in the AJAX settings, and no more parameters. This causes the wrong results to be returned, search isnt maintained, and neither is length.
I'm afraid that without having a page that I can debug, there isn't a use amount of help that I can offer. As you can see in my examples if you call the
draw()
function it will sent the parameters every time.This is the first case I've heard of where the parameters are not being sent, which I why I would need to be able to debug the page.
Allan
I have resolved this problem and, in case anyone else runs into this, it's caused by line 41 in the second code sample I gave above.
After setting $.ajaxSetup({ processData: false }); this causes dataTables to send [object Object] instead of parameterizing the parameters it should be sending ...
Woooops. :)
Thanks for your great work, support and quick response though Allan!