Cannot get StateRestore to work on page refresh using AJAX
Cannot get StateRestore to work on page refresh using AJAX

I am sorry if this has been answered already somewhere, but I cannot find a solution. At the moment I am trying to get the basic ajax example to work for me as described at https://datatables.net/extensions/staterestore/examples/initialisation/ajax.html
Using the relevant php files from https://github.com/DataTables/StateRestore/tree/master/examples/data I can successfully recreate the example.
The problem that I have (and it is the same as the example ) is that when you save a state it works until you refresh the page and then the state "resets". The pull down menu still shows the state exists, but is doesn't do anything.
To replicate on the example (assuming there are no existing states)
- Refresh page
- Create a state (State 1)
- Click on ID to reverse the ID order
- Create a state (State 2)
- Using the "Save States" pull down menu, confirm that by selecting the individual states that they work correctly (which they do).
- Refresh the page
- Using the "Save States" pull down menu and select "State 2". It no longer works as the ID column is not reversed.
For completeness I have added the response below that I get from my server.
Any help would be appreciated, thank you.
Replies
Yup, I see it too. Apologies for the error, I'm looking into it.
Allan
It is due to the ordering index (and other numbers) being stored as strings rather than numbers. DataTables has had enhancements that cause the restore not to work in this case (since it is a string, it can't find a column with that name).
I think I need to modify StateRestore to convert back to numbers where they should be. I don't like this - it should have been sent as a JSON object to the server, not HTTP parameters, so that type could be retained. That is something for StateRestore 2...
I'll post back later when I've had a chance to do this.
Allan
Hi Alan, thank you for your reply. As a newbie to this, I apologise if this is a silly idea, but if you don't want to change StateRestore at this stage, could the data not be manipulated in the PHP script?
Hi Paul,
I've just committed this change to address the porblem found here. It will be in the nightly build shortly.
I've also committed this change to DataTables core, due to an error I saw while looking into this.
You are right, the issue could be fixed in the PHP script, but since the Javascript sends HTTP parameters which are typeless, I don't think it would be right to expect the server to know to send back a specific type for certain properties.
Regards,
Allan
Hi Alan
Thank you for looking at this. Now that It is working, I have found a few other issues. I am not sure whether they are supported or not, but I have listed them anyway.
I am not sure how to add a test case onto the Datatables https://live.datatables.net site with PHP files so I have temporarily loaded a test scenario at https://dt.taskmgr.co.uk for you to look at. Not sure why but the link keeps taking you to "https://dt.taskmgr.co.uk/defaultsite", so just remove the "/defaultsite" on the browser and you should see it.
I have used the two php files and the 2500.txt file from Github. The other two files are listed below.
Issues
Quirks
Individual column search: A state is remembered after refresh, however the filter text is not displayed even though the filter is working. (I realise that this is how it has always been as I have seen comments on the forum about it). It is possible to remove the filter by selecting the appropriate footer box where the filter is and backspacing to delete the filter. (This obviously doesn’t change the “definition” of the state and the filter will be applied when the state is reselected.)
Files:
I hope that this helps, sorry its so long winded.
Hi,
Apologies for the delay in replying here.
Fixed with this commit, thank you!
That's because DataTables doesn't know anything about the input elements. There are two ways to address this:
stateLoaded
to populate the values into your column seach inputsOoo. That's a good one. Made me think for a little bit
. Fix committed here.
The final "quirk" should also have been resolved by the commits above
.
Huge thanks for the report and feedback.
Allan