Load state after api is available

Load state after api is available

Tatiana TotskayaTatiana Totskaya Posts: 1Questions: 1Answers: 0

I want to store datatable's state in database. It is possible and works perfect, when I use stateLoadCallback and stateSaveCallback. But as soon as another column is added to datatable the state is ignored and replaced by the default one.
So I decided that if I save state with column names not indexes then I can convert it to indexes when loading and back to names when saving. But unfortunately the api is not available on stateLoadCallback yet, so the conversion is not possible. Can you please suggest some workarounds?

Answers

  • allanallan Posts: 62,094Questions: 1Answers: 10,181 Site admin

    At the moment you would need to call page(), page.len(), etc as appropriate to set the state based on the state saved object. There currently is no API method that will restore the state at an arbitrary time after initialisation. That is something that will be added for the next major version.

    Allan

  • stevevancestevevance Posts: 58Questions: 6Answers: 1
    edited August 2017

    I would like to load a state object from a URL, so that people can share permalinks to specific views of the table.

    For example, this URL:

    http://localhost/companies.php#/?state={"time":1502772900456,"start":10,"length":10,"order":[[4,"desc"],[3,"desc"]],"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true},"columns":[{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"Sign%20Contractor","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":false,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}}]}
    

    It would be great if someone could copy and paste that URL and share it with someone else so they see exactly the same data in the table.

    This has at least one problem, I believe: On a table with many columns, you may exceed the number of characters allowed in a browser's location bar. There are also some properties that don't need to be here. For example, if there is no search applied on a column, does that column need to be present in the columns array?

    How I did that

    var hash = JSON.stringify(tableCompanies.state());
    $.address.parameter("state", hash);
    

    $.address is a plugin for jQuery that manipulates the "hash bang" style of URL hashes.

    This isn't the only way to solve the "permalink" need, I bet, but the one I was experimenting with today.

  • allanallan Posts: 62,094Questions: 1Answers: 10,181 Site admin

    For permalinks you might be better using the approach introduced in this blog post. Assuming the data in the table hasn't changed, it would work nicely (the state saving method would also require the data to not change).

    or example, if there is no search applied on a column, does that column need to be present in the columns array?

    Yes. It is index based. If you miss a column the others would be offset by one.

    Allan

This discussion has been closed.