keep saveState value on specific items

keep saveState value on specific items

K4nataaK4nataa Posts: 4Questions: 2Answers: 0
edited March 2021 in Free community support

I am trying to understand how to use the saveState method.
Righ now it is set on true, which means everything on the webpage will be saved. including the filter functions (it will only display the searched item and you can't undo that unless using $('table_Name').DataTable().state.clear();

with the stateSaveParams method you can specify which items you would like to save. The example is: data.search.search = " "; which works, but when I try customize something to my liking it doesn't work and I don't fully understand why.

I want to keep my iDisplayLength and the value upon refreshing. The rest should refresh upon reloading the page.
This is my current code:

$('#Container').DataTable({
                "scrollX": false,
                "iDisplayLength": 10,
                "aLengthMenu": [[10, 15, 20, 25, 50, -1], [10, 15, 20, 25, 50, "All"]],
                stateSave: true,
                stateSaveParams: function (settings, data) {
                    data.search.search = "", //Refreshes
                    this.api().columns([1, 3, 6, 13]) = " "; //
                    console.log(this.api().columns([1, 3, 6, 13])),
                    this.api().columns([1, 3, 6, 13]) = " ";
                },
            }).columns.adjust();
            $('#Container').DataTable().state.clear();
        });

i have select values on column 1, 3, 6 and 13 and I want those to be refreshed upon reloading the page.

I used the code from this discussion: https://datatables.net/forums/discussion/56816
I read up on the saveStateParms here: https://datatables.net/reference/option/stateSaveParams

I hope I formed my question correct. if I need to give any more information, feel free to ask.
Thank you in advance.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    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

  • K4nataaK4nataa Posts: 4Questions: 2Answers: 0

    hello, sorry for the late response.

    I have tried to make a live.datatables and here is the result: http://live.datatables.net/wukutiyi/1/

    When I filter through the items it works as intended. but when you wish to refresh the page and reset the selected items back to none. It doesn't do that.
    I want to keep the iDisplayLenght and aLenghtMenu value, but the <select> options will have to refresh as it causes problems.

    Hopefully this should give enough detail of my problem :)

    Kind regards,
    Brian

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    I'm not too clear what you're after, but if it's for the column searches to not be stored, you can remove those from the save data with

        delete data.columns;
    

    See example here: http://live.datatables.net/wukutiyi/2/edit

    Colin

This discussion has been closed.