Why DataTables not clear custom state?

Why DataTables not clear custom state?

crashworkcrashwork Posts: 3Questions: 1Answers: 0

I can't find an answer to such a question. I save custom state in the data object:

"stateSaveParams": function (settings, data) {
var temp = {};
jQuery('#userForm input.input-filter').each(function() {
temp[ jQuery(this).attr('placeholder') ] = this.value;
});
data.colsFilter = temp;
...
}
They are cleared by the state.clear() method, ("This method will trigger a DataTables state save but with an empty object (i.e. {}"), effectively erasing any existing state data.) but after the state expires (for example: "stateDuration": 50) only the standard DataTables values are reset to default, but my personal ones are not. Did someone have a similar problem?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,478Questions: 1Answers: 10,467 Site admin

    but my personal ones are not

    Once the state has expired, you shouldn't be getting the original object in any of the DataTables callbacks. Can you show me how you are accessing your custom properties?

    Thanks,
    Allan

  • crashworkcrashwork Posts: 3Questions: 1Answers: 0

    Hi, allan. Thanks for your reply. Here is an example of saving and loading inputs values.

    "stateSaveParams": function (settings, data) {
    var temp = {};
    jQuery('#userForm input.input-filter').each(function() {
    temp[ jQuery(this).attr('placeholder') ] = this.value;
    });
    data.colsFilter = temp;
    },

    "stateLoadParams": function (settings, data) {
    jQuery.each(data.colsFilter, function(key, val) {
    var temp2 = jQuery('#userForm').find('input[placeholder="'+key+'"]');
    temp2.val(val);
    });
    },

  • allanallan Posts: 63,478Questions: 1Answers: 10,467 Site admin
    Answer ✓

    Ah - I see the issue. Could you use stateLoaded instead of stateSaveParams please?

    It looks like the check for the duration is performed after the stateSaveParams callback is executed. I think that is wrong and I'll fix that. Thanks!

    Allan

  • crashworkcrashwork Posts: 3Questions: 1Answers: 0
    edited August 2017

    Maybe did you mean to use stateLoaded instead of stateLoadParams? If I use stateLoaded instead of stateSaveParams then stateLoaded is called only once when the page is updated, and as a result my settings are not saved.

    If I use stateLoaded instead of stateLoadParams this works correctly for me. stateSaveParams saves values after each change input and stateLoaded works correctly after state time expires.

  • allanallan Posts: 63,478Questions: 1Answers: 10,467 Site admin
    Answer ✓

    I did - sorry!

    Allan

This discussion has been closed.