Row select state save using local storage

Row select state save using local storage

glimpsed_chaosglimpsed_chaos Posts: 138Questions: 29Answers: 4

Using statesave with local storage, no problems.

Question - If a user selects a row, is there a way to save and then select that same row when stateload occurs? I searched and read about a possible solution back from 2011 but that was using cookies and Allen warned about size limitation. But it's 2020 and with local storage this seems like it should be easier.

I am using the select extension if that matters.

$(document).ready(function() {
    $('#example').dataTable( {
        "bStateSave": true,
        "fnStateSave": function (oSettings, oData) {
            localStorage.setItem( 'DataTables', JSON.stringify(oData) );
        },
        "fnStateLoad": function (oSettings) {
            return JSON.parse( localStorage.getItem('DataTables') );
        }
    } );
} );

Appreciate any help!

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • glimpsed_chaosglimpsed_chaos Posts: 138Questions: 29Answers: 4

    Here is my test case - http://live.datatables.net/movabaza/2/edit

    With the saveState option enabled for local storage, I can do a search for "Kelly", then reload and it stays filtered. As well I can see the local storage object:

    {time: 1591811695462, start: 0, length: 10, order: [[0, "asc"]],…}
    columns: [{visible: true, search: {search: "", smart: true, regex: false, caseInsensitive: true}},…]
    length: 10
    order: [[0, "asc"]]
    search: {search: "Kelly", smart: true, regex: false, caseInsensitive: true}
    caseInsensitive: true
    regex: false
    search: "Kelly"
    smart: true
    start: 0
    time: 1591811695462

    But this does not retain the selected row.

    A little more looking into the select extension, I should be able to use rowId: 'extn' to retain after reload though this does not appear to function in my test case either. I review the example for this and not sure why it works there.

    The select extension does not appear to use local storage for retaining the selected row information.

    Thanks again!

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    The stateSave option doesn't save the selected row state. This example by @colin does show how you can add the saved rows to stateSave:
    http://live.datatables.net/secukipe/1/edit

    I should be able to use rowId: 'extn' to retain after reload though this does not appear to function in my test case either.

    You are referring to this example. Its talking about reselecting rows when the data is reloaded, via ajax.reload(), not when the page is reloaded.

    Kevin

  • glimpsed_chaosglimpsed_chaos Posts: 138Questions: 29Answers: 4

    The example by Colin is great. Thank you Kevin!

This discussion has been closed.