How to manage stateSave value when we change the table?

How to manage stateSave value when we change the table?

lenamtllenamtl Posts: 265Questions: 65Answers: 1

Hi,

How to manage stateSave value when the table get changed?

I'm wondering what will be the best approach in the case we have stateSave values for a table
and the table get changed if we add, remove a column or change column order ....

Because when doing this datatables will throw an error if we don't clear the stateSave values...

Does the only available method is to clear the stateSave values?

As it is not easy to parse the stateSave value so I'm out of idea...

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @lenamtl ,

    The callback function, stateLoadCallback, is a good place to manipulate the saved data. You could probably detect if the table shape has changed and just destroy that saved information so that it starts fresh.

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    How do you detect the table shape have changed?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    By counting the columns and the rows. You could store the value that was relevant to the saved state in the saved data with stateSaveParams, and compare it again when it's reloaded.

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited November 2018

    It won't cover the case if a column was changed, let say the column was for address and get change to phone, so column equal the same qty as before...

    is there a way to save custom settings in stateSaveParams?

    Maybe having a way to version individual table could be another solution,

    For now, I will continue to delete the stateSave value using my update script as there is no easy way to update the stateSave values (this is not easy to parse the stateSave values) and may produce errors and delay for nothing if there is no changes..

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @lenamtl ,

    stateSave isn't that smart - see this example here. It only stores the current searches, orders, and paging position (plus a time check). So if the column types changed, it wouldn't know about it, or care even.

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited November 2018

    Yes I know this is why this is hard to parse...

    How would you compare both result ?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583
    Answer ✓

    Hi @lenamtl .

    If you take a look at the console in that example I posted, you'll see it's just a structure. So you can do checks like data.columns.length for example.

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    I figured out

    From Datatables

     console.log(
            'Number of all columns',
            $('#example').DataTable().columns().nodes().length
        );
    

    from LocalStorage

    var mycolumns = $('#example').DataTable().state().columns;
    var count= Object.keys(mycolumns).length;
    console.log(' localstorage ' + count);
    
This discussion has been closed.