stateLoadParams.dt not getting called after first load.
stateLoadParams.dt not getting called after first load.
$table.one( 'stateLoadParams.dt', function (e, settings, columnDataFromDt) {
if(columnDataFromDt['columns'][columnDefKey]){
columnDataFromDt['columns'][columnDefKey]['search']['search'] = columnDetails['column_filter_options']['attributes']['value'];
}
});
Hi, I have code like this, and when I do $dt.state.clear(); and window.location.reload()
The stateLoadParams.dt event never gets fired, until I run the code a 2nd time.
Is there a bug with this, or how am I supposed to deal with it, all I found was this:
https://datatables.net/forums/discussion/51369/stateloadparams-not-fired-in-first-visit
which seems to describe the problem, but I'm not really seeing a solution.
Do I need to initiailze the datatable after the page reloads?
Thanks
Replies
When you use
state.clear()
the saved state is empty, actually{}
. When the Datatable is initialized it fetches the saved state. If empty thestateLoadParams
event is not fired since there is nothing loaded. Here is a quick example to demonstrate:https://live.datatables.net/koxokale/1/edit
Use these steps:
There is no state loaded so the
stateLoadParams
event doesn't fire. I suspect this is expected behavior.What problem are you trying to solve when there is no state loaded?
Kevin
I have a page with a datatable and on the page there is a "Reset" button, which calls $dt.state.clear(); and window.location.reload();
I have a button called "Apply Setting" which will do an ajax call and load a new datatable.
Under normal circumstances, when clicking "Apply Setting", it will work, calling both the
and then the inside
columnDataFromDt['columns'][columnDefKey]['search']['search'] = columnDetails['column_filter_options']['attributes']['value'];
will get called, thus populating the filter.However, when I use the debugger if I click the "Reset" button, the state clears and the page reloads, the debugger only gets to the $table.one('stateLoadParams.dt' line and the anonymous function never gets fired.
Am I doing something wrong? Do you understand what I am getting at?
Is there a command I can run after the page reloads to ensure that the next time a datatable gets loaded that it will call 'stateLoadParams.dt'
Yes, because there is no state to load once its cleared. This is from the
stateLoadParams
docs:Does this make sense?
Sounds like you want to repopulate the column search values when clicking the "Apply Setting" button and that you want this to happen even if the state has been cleared. Is this correct?
Kevin
Yes Kevin,
that is correct, I am trying to Load a bunch of filters (search) when i click Apply Setting.
Which event would be most suitable? https://datatables.net/reference/event/
Thank you for taking the time to help me!
I was thinking about using the preDraw but it only takes (e, settings), whereas stateLoadParams takes function( e, settings, json ) and the json can be manipulated.
How would you do it?
To rephrase my understanding you want to clear the Datatable state but retain the column search values so on reload you still have access to these. If this is the case one option might be to use
stateSaveParams
to clear all but the search parameters you want to save. Use a global variable as a flag to determine whether to clear the state parameters or not. For example:Global flag:
stateSaveParams
:See the
stateSaveCallback
for details of the stateSave object.Reset event handler:
Kevin
I don't think that will work, how do I "undo" the dt.state.save() ?
My code is here, does this help at all? This is happening via ajax:
Here is an example of what I was describing:
https://live.datatables.net/koxokale/3/edit
If the flag is set it sets the state save object to the Datatables default values in
stateSaveParams
, but leaves the columns alone. This allowsstateLoadParams
to be called when the date is cleared. Butstate.clear()
is not used so there is a saved state.Does this help?
There is a lot to study in your code but it looks like you are looping through all the columns and trying to set the saved column search values based on data from the ajax request.
Kevin
I don't totally understand your solution, requirements or code. Maybe instead of setting the saved column search values based on the ajax response (assuming I understand what you are doing) maybe you can bypass this and just set the column search values directly. It doesn't seem like you are using the saved column search values but what is returned in the ajax response.
Kevin