Function to get stateSave data directly

Function to get stateSave data directly

hinderkshinderks Posts: 1Questions: 0Answers: 0
edited April 2014 in DataTables 1.10
Hi,

First of all, I wanted to thank you for your excellent work on this library. I've just started using DataTables, and I really love it. It's obvious that you've put an incredible amount of effort into this library.

Here is my question. Is it possible to do something like this: var data = api.getSaveStateData(); This would return an object with all the relevant data to save into localStorage (or where ever).

On a page with multiple widgets, it would be nice, to store all the data for the page in one localStorage key (rather than 1 key per widget).

[code]
// Save state of all widgets on page close
var pagedata = {
widget1: widget1.getSaveStateData(), // Some widget
widget2: widget2.getSaveStateData(), // Some other widget
dataTable: api.getSaveStateData() // A DataTable
};
localStorage["mykey"] = JSON.stringify(pagedata); // Save it all in one key
[/code]

Then, on page load, you would pass the relevant data into the widgets as initialization options.

I think the main benefit is getting the page save / load logic centralized in one place rather than spread around. Particularly if your state saving strategy involves AJAX, then it seems like it would be much better to save / load the page state with one request, rather than one request per widget. Another benefit to storing all the data in one key is a unified expiration time for all the page data, so that you don't end up loading a partially-initialized page where some widgets' data has expired and others not.

Is this something that is already possible, or maybe an easy change?

Thank you,
James

Replies

  • allanallan Posts: 61,692Questions: 1Answers: 10,101 Site admin
    What I think you would need to do is use fnStateLoad and fnStateSave to provide your own state saving functions (assuming I understand correctly). Then your own functions can store the state into whatever container you define, and read back from that container. If you wanted to have it store multiple tables into a single object, your state saving logic would simply need to do that.

    So not an easy change since it isn't built into the core, nor (as far as I am aware) has anyone done this before and published the code, but I don't expect it would be that difficult to do.

    Allan
This discussion has been closed.