Don't export hidden column

Don't export hidden column

jsonplusjsonplus Posts: 10Questions: 1Answers: 0

is there a smart code for do this:

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'copyHtml5',
                exportOptions: {
                    columns: [ 0, ':visible' ]
                }
            },
            {
                extend: 'excelHtml5',
                exportOptions: {
                    columns: ':visible'
                }
            },
            {
                extend: 'pdfHtml5',
                exportOptions: {
                    columns: [ 0, 1, 2, 5 ]
                }
            },
            'colvis'
        ]
    } );
} );

i need to not export hidden column for all button... in there a manner to define this rule only one (not reply for all button)?
I want define columns: [ 0, ':visible' ] for all button

my code actually is:

.....
               "dom": "Bfrtip",
               "buttons": [
                             "copy", "csv", "excel", "pdf", "print"
                            ],           
....

Thanks

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Each button is independent so you do need to define the exportOptions for each one. You can modify the defaults, as shown in this thread, but you would still need to do it for each button.

    One option, to avoid code duplicate is create an object that contains the exportOptions, then just use it for each, something like:

        var eo = {
            columns: [ 0, ':visible' ]
        };
    
        $('#example').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'copyHtml5',
                    exportOptions: eo
                },
                {
                    extend: 'excelHtml5',
                    exportOptions: eo
                },
                {
                    extend: 'pdfHtml5',
                    exportOptions: eo
                },
                'colvis'
            ]
        } );
    

    Colin

This discussion has been closed.