stateLoadCallback not working from DB
stateLoadCallback not working from DB
matissg
Posts: 63Questions: 15Answers: 0
I'm new to coding and Datatables and totally stuck as I cannot make my JS to load prepared JSON from DB:
{"time":"1495134965190","start":"0","length":"10","order":{"0":["0","asc"]},"search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"},"columns":{"0":{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},"1":{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},"2":{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}}},"ColReorder":["0","1","2"]}
and this is my JS:
jQuery(document).ready(function() {
var user_id = $("#data-table").attr('data-user-id');
var contname = $("#data-table").attr('data-controller-name');
$('#data-table').DataTable(
{
"processing": true,
"responsive": true,
"colReorder": true,
"serverSide": true,
"ajax": $('#data-table').data('source'),
"stateSave": true,
"stateSaveCallback": function (settings, data) {
$.ajax( {
"url": "/common/datatables_states/"+contname+".json",
"data": {"common_datatable_state":{"user_id": user_id, "name": contname, "state": data}} ,
"dataType": "json",
"method": "PATCH",
"success": function () {}
} );
},
stateLoadCallback: function (settings, callback) {
$.ajax( {
url: "/common/datatables_states/"+contname+".json",
async: false,
dataType: "json",
method: 'GET',
success: function (json) {
callback( json );
}
} );
}
}
);
});
Link yo my Debug is this.
My stateSaveCallback is working as I see in my terminal it saves state, however I cannot get stateLoadCallback wokring I'll be happy for any help. Thank you!
This discussion has been closed.
Answers
These should be integers rather than strings.
What happens when it is loaded - do you get any errors?
Allan
I'm using Rails and in my terminal I don't see any error. It looks like this:
My stateSaveCallback saves JSON like this:
Could you try removing:
from the Ajax call you are making. The latest DataTables will handle an async load okay with the callback function called.
Failing that, can you link to a test case showing the issue please?
Thanks,
Allan
HI Allan,
Disabling
async: false,
did not work. I even triedstateLoadCallback
withstateSaveCallback
commented, but it does not help as well.For example, I search record by "big group" and my JSON looks fine to me:
As you can see my search value is there, so on page refresh I should be seeing my Datatables populating particular search as it has to loaded from DB, but it does not happen. It just renders regular table. stateSaveCallback however is working just fine - writes JSON to DB as expected.
I've been inspired by this blog post on stateSave/Load and this live demo. That's PHP version, so I'm trying to adapt this to my Rails app + there is older version of Datatables used.
Do you see anything I could be checking in addition to what I've done? By "test case" did you mean some live demo?
Yes.
https://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read#latest
I don't see anything else obvious from the above. A link to a test case probably would be needed to help resolve this.
Using the debugger might give us the information to resolve the issue, but I'm not sure.
Allan
So I just stumbled upon this issue as well. It looks to me like there is a clause in the datatable code that rejects any stateloadcallback if the 'time' field is older than 2 hours.
When the user saves their settings I grab their local storage item and put the entire statesave in the database. To fix this 'reject old data issue' I just set the 'time' field coming back from the database equal to the current time like below: