StateSave custom key

StateSave custom key

svashtarsvashtar Posts: 8Questions: 3Answers: 0

Hi,

i have many datatables in my app.
Some users can also switch users (testing etc...), and because of the stateSaving (at least i think that is a reason), when switching a user, tables are sometimes displayed wrong.

I think this is due to the sateSave as the table state is saved > user is switched > stateSave is loaded from previous user (as it is basically same computer/browser and i guess the key is just the same, and datatables has no way to know about this change). This causes a problem, when datatable is user-related (like permissions, settings, etc...).

I would like to set custom stateSave key.
I was thinking about leaving datatables original key name (Datatables_TableID_Url) but also append/prepend user ID...

How can I do this?
A fine solution would also be to re-create original key manually but i have no idea how to access current table ID.
As all my tables have different IDs, url part would not even be neccessarry, but nothing wrong with it...

Thx

Answers

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    You will have to use stateSaveCallback and stateLoadCallback to define your own storage location.
    Datatables currently only uses the table id and the path as the identifier for the state saving and there is no option to configure that.

    Thanks

    Tom

  • svashtarsvashtar Posts: 8Questions: 3Answers: 0
    edited June 2016

    Hi. Thanks!
    Already figured it out.
    What I was actually searching for was sInstance for the table identification.

    If someone else needs something similar, here is what i have now:

    state_key = window.location.pathname + "-" + USER_UID
    
    "stateSaveCallback": function(settings,data) {
      localStorage.setItem( 'DataTables-'+settings.sInstance+state_key, JSON.stringify(data) )
    },
    "stateLoadCallback": function(settings) {
      return JSON.parse( localStorage.getItem( 'DataTables-'+settings.sInstance+state_key ) )}
    

    Works perfectly.

This discussion has been closed.