DataTables state() set function

DataTables state() set function

sesplersespler Posts: 12Questions: 6Answers: 0
edited June 3 in Free community support

I have been using the state() function to get the state, and then modify one of the columns' visibility and set the state of the table back to that modified state with table.state(x); table.draw(false);

This has worked for me on v2.x, and I see that on the state() documentation page that this was available since v1.10, but I have not been able to make it work on v1.13. I'm wondering if the set state overload is something that is only available in later versions and it was just not mentioned there?

Thanks.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 22,015Questions: 26Answers: 5,081

    Setting the state() in this test case works with 1.13.11:
    https://live.datatables.net/kawobuvu/1/edit

    Do you get console errors with trying to use state() with 1.13? For help debugging please update my test case or provide a test case showing the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • sesplersespler Posts: 12Questions: 6Answers: 0

    If you go to your test case even, you can open the console and see an error when you rerun. It will say:

    "TypeError: table.state(...).draw is not a function

    I believe this is happening because the overload with the state as an argument doesn't exist, and it defaults to running the normal state() function, which is returning the state of the table, not the DataTables API, on which of course .draw() does not exist.

    Here's another example that further illustrates this:
    https://live.datatables.net/kawobuvu/2/edit

  • kthorngrenkthorngren Posts: 22,015Questions: 26Answers: 5,081

    I got bit by the saved state of testing first with DT 2.3.1 then changing to 1.13.11 :smile:

    I don't know where the 1.x docs are or if they are available to see if state() can be used as a setter. @allan can confirm and maybe update the docs to indicate the setter option is only available in 2.x.

    Maybe you can do something like this instead:

    var state = table.state();
    table.column(2).visible( !state.columns[2].visible );
    

    https://live.datatables.net/kawobuvu/4/edit

    Kevin

  • allanallan Posts: 64,520Questions: 1Answers: 10,664 Site admin
    Answer ✓

    In 1.x no, it cannot be used as a setter. I do actually have a since flag for the setter method in the source XML, but it looks like I don't render that into the HTML docs. Sorry about that - I'll fix that.

    Allan

  • sesplersespler Posts: 12Questions: 6Answers: 0
    edited June 3

    Oke doke thank you

Sign In or Register to comment.