Applying a state after DT initialization or supporting AJAX in stateLoadCallback

Applying a state after DT initialization or supporting AJAX in stateLoadCallback

dclar43dclar43 Posts: 47Questions: 13Answers: 2

As noted with the open() method documentation "Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform...". stateLoadCallback's documentation suggests forcing a synchronous request to load a state from the server.

Is there a known way to avoid forcing this request to be synchronous? Ultimately I would prefer just leaving it as AJAX, even if there is a slight delay on initialization.

Some alternatives I've been looking at:

  1. A method of loading a state post-initialization. I was unable to find one within DataTables
  2. Returning the state packed in DataTable's own ajax request and assigning it to a variable referenced in stateLoadCallback. Kind of ugly, would like to avoid this.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin
    Answer ✓

    A method of loading a state post-initialization. I was unable to find one within DataTables

    This github thread discusses a plug-in method to provide that ability.

    With sync requests being put aside, this might be something that DataTables core has to provide.

    Returning the state packed in DataTable's own ajax request and assigning it to a variable referenced in stateLoadCallback.

    That would require an API method as well.

    The other option is to use $.ajax to get the state data and then initialise the table - returning the state in stateLoadCallback from a local variable.

    Allan

  • dclar43dclar43 Posts: 47Questions: 13Answers: 2

    Thanks for the info Allan!

    I use the second method on the old version of my app and I suppose I'm just hung up on it being ugly. I think I'll survive if it comes to that.

    To convince me? A few more people asking for it!

    Mark me down. In case another use-case is handy:

    Prior to doing a slight rewrite while switching to Editor I've handled the table's state on my own. This has been a feature my users have given great reviews on. My users track projects through a DataTable. All of these projects are similar enough that they don't warrant their own table. Users in certain positions or parts of the company are often only concerned about a subset of these projects. Being able to offer a list of table states that apply filters to show the users only the projects that they care about will be a necessity as the data set grows.

  • sansoo22sansoo22 Posts: 3Questions: 0Answers: 0

    I know this is old but I've been searching for a way to do this for over a week. My application that uses datatables requires the exact same features as dclar43. I let my users build and save their own "filters" and now all i need is a way to load these on demand. With the buttons plugin my users can get a csv of the filtered table whenever they want. A feature that lets me load a filter on demand would keep me from having to write spreadsheet code ever again.

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    Thanks for the feedback - much appreciated. A state load method is something I plan to introduce in future (although can't put a timeline on it at the moment).

    Allan

  • sansoo22sansoo22 Posts: 3Questions: 0Answers: 0

    I ran into a small issue using this plugin https://github.com/DataTables/DataTables/issues/630 for state loading. I don't know if its just my implementation of datatables or an actual "feature". I had to use "stateLoadParams": function(settings, data) { data.time = new Date().getTime(); } in my initialization or it seemed datatables would reject the state after a while and oLoadedState would be null. I spent 2 hours tearing my code apart before I randomly tried this. Leaving this here just in case I'm an idiot or this really is a necessity and other can find it helpful.

  • dclar43dclar43 Posts: 47Questions: 13Answers: 2

    FWIW I ended up doing the following:

    I just grab the state as passed into the stateSaveCallback option and run an AJAX request to send it server-side as JSON. Then I just store it in a database(naughty naughty but MySQL will soon support a JSON data type and other DBs already do) associated with the user and a name for the state(default or as a custom name). Loading does the exact same thing all using the aforementioned plugin.

    I haven't run into the timing issue that sansoo22 mentions. I do save the user's default state each time stateSaveCallback is called if that differs from your implementation.

This discussion has been closed.