Set table condition(s) back to default

Set table condition(s) back to default

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I was wondering if there was a way to set the table back to the state it was in when the page loaded, and if this can be done for specific settings. Meaning if theres a DataTable:

$('#example').DataTable({
    lengthMenu: [ 5, 10, 25, 50, 75, 100 ],
    pageLength: 25,
    order: [[3, 'desc']]
});

And the viewer searches for a string, and sets the page length to 10, and orders the first column ascending.. Is there an API method to set the tables conditions back to default? (Meaning: length 25, order 3rd col desc, no search string)

Also, if this needed to be done to the conditions specifically (as opposed to the whole table), whats the easiest way to set them to default? Such as the Order, or Length. I know that executing page.len() or order() without any parameters just errors out, it results in either:

Uncaught TypeError: Cannot read property 'len' of undefined

or

Uncaught TypeError: table.page.len(...).draw is not a function

Obviously its possible to poke through the settings() and get the value for each, but I was hoping for an easier way.

So any help would be appreciated.

This question has an accepted answers - jump to answer

Answers

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

    Currently you would need to set each property in turn using the API. I'm going to be modifying the state save parameters method in future so it can be run at any time (i.e. get the current state) and probably adding a state load method so you can load a saved object. That's not likely to happen before the new year though.

    I know that executing page.len() or order() without any parameters just errors out, it results in either:

    It shouldn't: http://live.datatables.net/zehutepo/1/edit

    Allan

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

    I would probably suggest writing a custom API plug-in that will get the values from the public API (page.len() etc) and return all the info you need in a single object. You could do likewise for setting.

    There should be no need to use the settings method.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited December 2015

    It shouldn't

    The order() does if theres no value (JSbin), I was saying if theres no value, instead of setting it to default, it will error out.

    There should be no need to use the settings method.

    But I'm talking about getting the values of the settings for the DT instance as set when the table was initialized (defaulted or not). Say I have a lengthMenu of 5, 10, 15, and I set the pageLength to 10, then use page.length() to set it to 5.. How can I use page.length() to get the value that it was at default? (which is 10). I know that in the settings(), I can get the default values that may have been set when initializing the table, I think I would have to see if they are in the oInit object, if so, use that value, otherwise, look in $.fn.dataTable.defaults

    If theres a way to get the default settings for each condition (Length, page, order, etc), then that would be awesome, lmk!

    Thanks allan, as always

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

    table.order().draw();

    The issue here is that order() with no parameters does not return an API instance (see order()) it returns an array - so there is no draw() method that can be chained on.

    But I'm talking about getting the values of the settings for the DT instance as set when the table was initialized (defaulted or not).

    There is no option for that as it isn't something that I've ever required before. You could possibly simply listen for the init event and then get the required values from the API and store them somewhere - that would mean you wouldn't need to resolve the defaults and configuration options (which I don't actually think would be possible anyway since defaults could change after that table has been initialised!).

    Allan

This discussion has been closed.