Set particular colvisGroup button as default on first page load

Set particular colvisGroup button as default on first page load

rdmrdm Posts: 192Questions: 54Answers: 4

In my scenario, I have a set of colvisGroup buttons that work as intended when you click any of the buttons.

By default, my table shows all columns. Is there a way I could set a particular colvis button to be loaded by default?

These are my button definitions in the configuration section, where the show and hide definitions are supplied by variables. This table has 50 columns, which is why you're not seeing the definitions inline.

Is there a configuration property I can set so that a particular button, say Student is selected on first page load?

$('#example').DataTable({
    dom: "Blfrtip",
    ajax: { /* ... code ... */ },
    serverSide: true,
    stateSave: true,
    buttons: [
        {
            extend: 'collection',
            text: 'Column Visibility',
            buttons: [
                {
                    extend: 'colvisGroup',
                    text: 'All',
                    show: allShow,
                    hide: allHide
                },
                {
                    extend: 'colvisGroup',
                    text: 'Student',
                    show: studentColsShow,
                    hide: studentColsHide
                }
                /* ... more buttons */
                
            ]
        }
    /* ...  more code ... */
    });

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Answer ✓

    Is there a way I could set a particular colvis button to be loaded by default?

    Do you mean you want that button to trigger the default display of columns?

    Two options:

    1. Use columns.visible to match that button.
    2. Use button().trigger() when the table has been loaded.

    Allan

  • rdmrdm Posts: 192Questions: 54Answers: 4

    Option 2 looks like the best. No sense having to define the same thing twice when the table has 50 columns.

This discussion has been closed.