Datatables with API Storage for selected entries, problems with reload
Datatables with API Storage for selected entries, problems with reload
TL:DR: Prevent ajax.reload() from calling the "selected" event.
I have an issue regarding a datatable, where i retrieve data from an API, which also serves as a storage for the selection of rows.
This way, when the client will reload the page or wants to edit his selection later on, his selection will be stored. Secondly, we can use the selection in the next steps of a process, without the need of hidden input fields to submit them to the Backend.
Now each time i have a selection inside the datable, comming from an external source like a button or a search field, i will send the selection to the api and call $dataTable.ajax.reload();
This causes the datatables to refresh the content and call the select function on every previously selected row, even though these have changed in the api. I dont want this to happen.
So how can i prevent ajax.reload from calling the "selected" event?
Regards
Dominik
This question has an accepted answers - jump to answer
Answers
I checked after the API call, how many rows are selected by calling
console.log($dataTable.rows( { selected: true } ).count());
Suprisingly the count after deselection is 0. So somehow the "selected"-class is not removed from the row or is set later on in a strange manor.
Does anyone now how this can happen or how i can solve it?
Client Log /JS
Server Log
This is how Select retains the selected rows when
ajax.reload()
is called. Basically it will attempt to find the ids of the selected rows and then once the reload is complete it will then reselect them.I can't be sure without a test case, but I suspect that is what you are seeing.
This isn't really a way to disable that at the moment - it isn't a configurable option. You could not have row ids in which case it wouldn't find anything. Or modify the library and remove that code (assuming I'm correct and it is that which is causing the issue).
Allan
I think you have interpreted it right.
For now i just implemented my own local data-structure, which keeps track which rows have been selected and which not.
Because this problem only occurred when selecting multiple entries at the same time via AJAX, i now return the selected/deselected entries from my API-endpoint. According to the call, I then add/remove the specified values from the local data-structure.
Additionally I decided to distinguish between
select
anduser-select
event. When theselect
event is being called, I check if the value exists in the local data-structure. If not, I deselect it. Onlyuser-select
Event is then allowed to add data to the local data-structure.Also wanted to paste my code. Might help someone in the future! (Hello person from the future)
Data-Structure
Event-Handler
Sync-Progress-Events
API-Calls
Thanks for posting your code. Good to hear you've got it working the way you need now!
Regards,
Allan