stateSave Logic Not Working

stateSave Logic Not Working

tkocurektkocurek Posts: 3Questions: 1Answers: 0

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

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    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

  • tkocurektkocurek Posts: 3Questions: 1Answers: 0
    edited December 2015

    Hey Allan,

    The page in question is password protected. Yes, the #tracking_view_columns is being populated on page load and stateLoadCallback properly parses the data. However, immediately after stateLoadCallback is called, stateSaveCallback is called and the data passed in is not what was just parsed by the stateLoadCallback, 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?

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    You could drop me a PM with the login details by clicking my name above and then the "Send message" button.

    Thanks,
    Allan

  • tkocurektkocurek Posts: 3Questions: 1Answers: 0

    Allan,

    I'm sure you get this all the time but it was user error. I traced it back to _fnLoadState.

    // Number of columns have changed - all bets are off, no restore of settings
    if ( columns.length !== state.columns.length ) {
        return;
    }
    

    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 returning undefined and the conditional was failing. Sorry about that.

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    No problem - great to hear you've got it sorted!

    Allan

This discussion has been closed.