Loading state works, unless state hasn't been saved yet
Loading state works, unless state hasn't been saved yet
nesadi
Posts: 2Questions: 1Answers: 0
Been trying to figure this out for a week and I'm at my wit's end. I changed a table to save and load its state through AJAX, but if I start from a new session and the state hasn't been saved yet (so that there's nothing to load), then the table doesn't load at all (no table shows up on the page). How do I get it to ignore the retrieved Json data (which is {} anyway)? These are the relevant parts of my table. The stateLoadParams part was my latest attempt that failed.
stateSave: true,
stateSaveCallback: function (settings, data) {
$.ajax( {
url: "{% url 'finder:save_datatable' %}",
data: JSON.stringify(data),
dataType: "json",
type: "POST",
success: function () {},
});
},
"stateLoadParams": function (settings, data) {
if (Object.entries(data).length === 0 && data.constructor === Object) {
return false;
}
},
stateLoadCallback: function (settings, callback) {
$.ajax( {
url: '{% url "finder:load_datatable" %}',
dataType: 'json',
success: function (data) {
json = JSON.parse(data);
callback( json );
},
});
},
This discussion has been closed.
Answers
After further googling, I've found that the answer to my problem is here https://datatables.net/forums/discussion/50606/first-time-stateloadcallback-is-called
I just needed to add a callback(null) or callback(data) before letting it get parsed to my LoadCallback function in case the returned JS object is empty.