Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

State save clear #255

Closed
DataTables opened this Issue · 16 comments

6 participants

@DataTables
Owner

Might be useful to have an API method which will clear the save state - based on mars' comment here: http://datatables.net/forums/discussion/8096/localstorage-for-state-saving-and-updates-in-1.9#Item_37

@whogy

does anybody need this feature?

@DataTables
Owner

Its a feature request that pops up now and then, so yes I'm sure some would benefit from it, but I'm not yet sure if I'm going to include it in DataTables core. The way I'd like to see it implemented is in the new API with the following methods defined:

state.save()
state.data()
state.clear()
@whogy

thanks indeed.

@KyleRoss

I'd like to see this feature also so we will not have to manually kill all the cookies when the user logs out from our applications. There are other use cases for it also, but I do not need to go into the details. It definitely would be useful though.

@marcovit79

I'm writing a web page where the user can load page column order and visibility from db.
Now I have to reload the page
If datatable has such features I can apply configuration without page reload.

@mandeepsg3

Hi,
Has this been added in 1.10 .. I dont dont see any option to clear local storage in v1.10. If it has been added can someone post sample code on how to clear localstoreage.

@DataTables
Owner

No it hasn't - that is why the bug is still open. I will be looking at adding this soon.

@KyleRoss

If you are using the built in state saving functions of DataTables, then you can place the following somewhere in your code after the DataTables code:

(function($){
    $.fn.dataTable.Api.registerPlural('tables().state.clear()', 'table().state.clear()', function(opts) {
        return this.iterator('table', function(ctx) {
            try {
                (ctx.iStateDuration === -1 ? sessionStorage : localStorage).removeItem(
                    'DataTables_'+ctx.sInstance+'_'+location.pathname
                );
            } catch(e) {}
        });
    });

    $.fn.dataTable.Api.register('state.clearAll()', function() {
        var clearStorage = function(key, storage) {
            if(key.match(/^DataTables_/) !== null) {
                try {
                    storage.removeItem(key);
                } catch(e) {}
            }
        };

        try {
            $.each(window.localStorage, function(key) {
                clearStorage(key, window.localStorage);
            });

            $.each(window.sessionStorage, function(key) {
                clearStorage(key, window.sessionStorage);
            });
        } catch(e) {}

        return this;
    });
}(jQuery));

This will add a couple of new methods to the API.

table().state.clear() - Clear any saved state information for a specific table.
tables().state.clear() - Clear any saved state information for all/multiple tables on page.
state.clearAll() - Clear all saved state information for all DataTables (including tables not rendered on the current page or on other pages in the same domain).

Example:

$(document).ready(function() {
    var dt = $('#DataTable1').DataTable();

    // Clear save state for #DataTable1
    dt.table().state.clear(); // You can also call draw() here if needed

    // Clear save state for #DataTable2
    dt.table('#DataTable2').state.clear();

    // Clear save state for all tables on the page
    dt.tables().state.clear();

    // Clear save state for all tables on the domain
    dt.state.clearAll();
});
@DataTables
Owner

Hi, thanks for this. Funnily enough I'm working on this at the moment. Writing the documentation for the new methods which are:

  • state() - get last saved state
  • state.save() - trigger a state save
  • state.loaded() - get state that was loaded when the table was initialised
  • state.clear() - clear the state
@KyleRoss

@DataTables Haha, perfect. I was in need of it so I figured I would just go ahead and put something together. Beat me to it ;)

@DataTables
Owner

Heh - bug fixes are like buses. Wait for months and then they all come at once...

Commit for the src repo

And this repo is currently syncing up and I'll rebuild the nightly shortly.

Let me know if you hit any problems with the new methods!

@DataTables DataTables closed this
@antgar9

I think you have forgotten to add those methods to JQuery API.
I mean, they works with .DataTable(), but not with .dataTable()
Thanks.

@DataTables
Owner

No - the legacy API will not have new features added to it. Indeed the legacy API is no longer documented on the site (although the old docs are still available at http://legacy.datatables.net ).

The intention is that .dataTable() should just return a plain jQuery object while .DataTable() will return a DataTables specific API.

@antgar9

Maybe this doc should explain this difference better, when it says "The result from each is an instance of the DataTables API object", I thought both were the same. So dataTable is the deprecated legacy api.
Now I get it ... thanks for the clarification.
Great tool.

@DataTables
Owner

"The result from each is an instance of the DataTables API object"

That is an accurate statement. The documentation specifically lists:

  • $( selector ).DataTable();
  • $( selector ).dataTable().api();
  • new $.fn.dataTable.Api( selector );

Note the .api() on the second one.

It then also goes on to say:

It is important to note the difference between $( selector ).DataTable() and $( selector ).dataTable(). The former returns a DataTables API instance, while the latter returns a jQuery object.

I'm not sure what is unclear about that. How can it be improved?

@DataTables
Owner

:-). No worries. Thanks for keeping me on my toes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.