How to prevent columns visibility state when adding new columns ?

How to prevent columns visibility state when adding new columns ?

iiznoiizno Posts: 2Questions: 0Answers: 0

Hello,

Thank you for all the work you put in that project !

We let our user choose which columns they can show or hide with the colvis plugin. But we add or remove columns a lot and each time we do, every user need to configure again which column he want to hide. Is there a way to prevent that reset ?

Replies

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416

    https://datatables.net/reference/option/stateSave

    is what you need.

    But we add or remove columns a lot and each time we do, every user need to configure again which column he want to hide.

    Not sure what you mean by this. Are you changing your code a lot? If so: Of course the users won't have those changed columns in the saved state because they might not have existed previously.

    I let my users choose whether they want state saving or not. If they want it I use stateSave: true. Otherwise it is set to false. I also have a "reset" button for the users to be able to delete all saved layouts.

  • iiznoiizno Posts: 2Questions: 0Answers: 0

    Thanks for your response !

    Let me try to explain it again.

    We add new columns each month, as more data are added to our model which are updated often with more context or fields. We let our user use the Column Visibility button to chose which column they can display.

    Bu each time we add a new column in a new commit, The column visibility state is completely reset for the user.

    I completely agree and understand that the new colum we just added is not in the saved state. But I don't want all the saved states to reset

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416
    edited July 2020

    That is a very special case I see. You might have to manipulate the saved state then. Editor provides a couple of methods that could be helpful for this.

    I use something similar: I have the same data table on three different pages with different configurations on each page. That caused a problem because the "normal" saved state is per data table not per page where the data table is used. Hence I used stateSave and stateLoad Callbacks.

    Here is the relevant part of my code with different Item names for the three different pages. You might use these callbacks to append the data just for the newly added columns etc.. Just take a look at the settings and data objects used. You'll figure it out, I guess.
    https://datatables.net/reference/option/stateSaveCallback

    stateSaveCallback: function(settings,data) {
        if ( ctrMgmtServerSidePage ) {
            localStorage.setItem( 'ctrMgmtServerSidePage_' + settings.sInstance, JSON.stringify(data) );
        } else if ( inboxExpPage ) {
            localStorage.setItem( 'ctrInboxExpPage_' + settings.sInstance, JSON.stringify(data) );
        } else {
            localStorage.setItem( 'ctrMgmtClientSidePage_' + settings.sInstance, JSON.stringify(data) );
        }
    },
    stateLoadCallback: function(settings) {
        if ( ctrMgmtServerSidePage ) {
            return JSON.parse( localStorage.getItem( 'ctrMgmtServerSidePage_' + settings.sInstance ) );
        } else if ( inboxExpPage ) {
            return JSON.parse( localStorage.getItem( 'ctrInboxExpPage_' + settings.sInstance ) );
        } else {
            return JSON.parse( localStorage.getItem( 'ctrMgmtClientSidePage_' + settings.sInstance ) );
        }
    },
    

    https://datatables.net/reference/option/stateSaveParams
    may also be an option. I would also search the forum very intensively.

    Here is another thread on this:
    https://datatables.net/forums/discussion/comment/143440/#Comment_143440

    I am sure you'll find more just by searching the forum.

This discussion has been closed.