StateRestore Ajax loaded states doesn't work
StateRestore Ajax loaded states doesn't work
I have a problem with select restored state.
I am using DataTables 1.12.1 and StateRestore 1.1.1
I am using Ajax to store states in database, saving and loading working fine but when I try to select one of loaded states - nothing happens.
In my config I have:
"stateSave": true
as also:
`buttons: [
'colvis',
'createState',
{
extend: 'savedStates',
config: {
ajax: '/datatable/state-restore/',
rename: false,
}
}
],
When I create state, POST is going to the endpoint and success returned.
When I refresh page, at the beginning there is also POST and saved states are loaded:
State 1: {c: {ajax: "/datatable/state-restore/",…}, page: 0, time: 1653554456731, order: {0: {"": "asc"}},…}
c: {ajax: "/datatable/state-restore/",…}
columns: {0: {search: {regex: "false", smart: "true", search: "", caseInsensitive: "true"}, visible: "false"},…}
length: 25
order: {0: {"": "asc"}}
page: 0
search: {regex: "false", smart: "true", search: "", caseInsensitive: "true"}
start: 0
stateRestore: {state: "State 1", tableId: "common-table", isPreDefined: "false", isPredefined: "false"}
time: 1653554456731
State 2: {c: {ajax: "/datatable/state-restore/",…}, page: 0, time: 1653557487659, order: {0: {"": "asc"}},…}
c: {ajax: "/datatable/state-restore/",…}
columns: {0: {search: {regex: "false", smart: "true", search: "", caseInsensitive: "true"}, visible: "false"},…}
length: 25
order: {0: {"": "asc"}}
page: 0
search: {regex: "false", smart: "true", search: "", caseInsensitive: "true"}
start: 0
stateRestore: {state: "State 2", tableId: "common-table", isPreDefined: "false", isPredefined: "false"}
time: 1653557487659
They are visible on saved states list:
https://i.ibb.co/THpt2RV/obraz-2022-05-26-113522649.png
But then when I select one of them - nothing happens, like I wrote earlier.
Can anyone help?
I will just add that if I don't use Ajax and states are stored in LocalStorage - then it works fine.
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be [found in this guide](https://datatables.net/manual/tech-notes/8#Code!
Answers
The snippet from the Ajax load looks the same but not all of the data is shown. Have you compared them - is there a difference?
As a simple test, sort one of the columns then create a new state. Reload the page and look at the JSON response for that state to see if order has changed. Try selecting it - does the order change to the saved state?
This example works. Can you post a link to your page or a test case replicating the issue so we can take a look?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin