stateSave Logic Not Working
stateSave Logic Not Working
I am manually persisting the state of the DataTable to the DB and the populating the settings in the view for parsing.
...
stateSave: true,
stateLoadCallback: function(settings) {
return JSON.parse($('#tracking_view_columns').val());
},
stateSaveCallback: function(settings, data) {
save_settings(tracking_view, data);
}
...
The save_settings
method is:
function save_settings(tracking_view, data) {
$('#tracking_view_columns').val(JSON.stringify(data));
$.ajax({
type: 'POST',
url: "/users/#{current_user.id}/save_settings",
data: {
setting_tracking: {
view: tracking_view,
cols: data
}
}
})
}
Saving the state works fine, as well as the population of the state settings in the view. However, I see when the page is refreshed that the stateLoadCallback
returns the correct saved settings, then the stateSaveCallback
is called and passes the wrong state settings to the save_settings
method. It is passing the default state settings. Why is stateSaveCallback
being called on page refresh and why isn't it saving the settings I loaded initially?
Answers
Could you link to a page showing the issue so I can debug it please?
I assume that
#tracking_view_columns
is being populated by the server-side on load (i.e. not by Ajax)?Allan
Hey Allan,
The page in question is password protected. Yes, the
#tracking_view_columns
is being populated on page load andstateLoadCallback
properly parses the data. However, immediately afterstateLoadCallback
is called,stateSaveCallback
is called and the data passed in is not what was just parsed by thestateLoadCallback
, it's reset to default values (ie all columns visible and default order). I am using colVis and colReorder.How have others given you access to debug in the past when their implementation is sensitive data behind authentication?
You could drop me a PM with the login details by clicking my name above and then the "Send message" button.
Thanks,
Allan
Allan,
I'm sure you get this all the time but it was user error. I traced it back to
_fnLoadState
.It when saving to the DB, for some reason the
columns
Array property was being converted to an Object. So when I was parsing it back out,state.columns.length
was returningundefined
and the conditional was failing. Sorry about that.No problem - great to hear you've got it sorted!
Allan