Callback action is not firing for columnsToggle bu

2019-07-03 08:23发布

问题:

In the following JSFiddle the action function does not fire whenever a button to select a column in the column visibility button is selected. Below is the code that I am using:

$(document).ready(function() {
var table = $('#example').DataTable( {
    dom: 'B',
    "buttons": [
                {
                    extend: 'colvis',
                    postfixButtons: ['colvisRestore'],
                    buttons : [{
                        extend: 'columnsToggle',
                        action: function (e, dt, node, config) {
                            alert('Activated!');
                            console.log("Activated!");
                        },
                    }],
                }
            ],
    }
);} );

I would really appreciate your help on this one.

回答1:

CAUSE

Button columnsToggle doesn't have action option as opposed to colvis button.

SOLUTION

Handle column-visibility event which is fired when the visibility of a column changes.

$('#example').on('column-visibility.dt', function(e, settings, column, state ){
   console.log('Column:', column, "State:", state);    
});

DEMO

See updated jsFiddle for code and demonstration.