state.loaded() returns null

state.loaded() returns null

paul_009paul_009 Posts: 3Questions: 1Answers: 0

Hello,
I'm trying to use state.loaded() to get the state of the table when it was first loaded during initialisation. I set stateSave: true, but when I try to call state.loaded() I get null.

Is there anything else that I should've done to get the initial state with state.loaded().

Answers

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    Maybe you are calling it before the Datatable is initialized. Try using it in initComplete, like this:
    http://live.datatables.net/valafaze/1/edit

    Kevin

  • paul_009paul_009 Posts: 3Questions: 1Answers: 0

    Thanks for your fast answer! I'm also getting a null inside initComplete
    Does this have anything to do with the browser and the how the state is stored?

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    Can you post a link to your page or a test case showing the issue so we can see what you have and help debug?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    This example uses ajax to store the state. You can see the initComplete is called after the stateLoadCallback.
    http://live.datatables.net/xeyutize/5/edit

    Have you verified the the state is being saved and loaded form where you are saving it? It might be null because the its not saved or fetched properly.

    Kevin

  • paul_009paul_009 Posts: 3Questions: 1Answers: 0

    I'm just initiating the table with stateSave: true. Afterwards I'm selecting the table using jQuery.
    I can access the state with table.state() but table.state.loaded() returns null.
    I didn't explicitly save the state anywhere. As I understood from the documentation it is saved in localStorage

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    edited February 2021

    When you reload the page is the table state updated correctly?

    Maybe there is a bug in the Datatables version you are using. What version do you have? You can try it with the test case to see.

    I'm just initiating the table with stateSave: true. Afterwards I'm selecting the table using jQuery.

    Make sure the selector is correct. If incorrect the result could be null, see this updated example:
    http://live.datatables.net/valafaze/4/edit

    Maybe you can update my test case to show the issue. Without seeing the problem it will be difficult to debug.

    I'm also getting a null inside initComplete

    Did you use the statement that I had in my example or something different?

    Kevin

This discussion has been closed.