Search code examples
phpjquerydatatablesdatatable-buttons

Exclude column from export in Datatables Buttons


I have the following initialization for my datatables:

$(document).ready(function() {
    $('.datatable').dataTable({
       dom: 'Bfrtip',
        buttons: [
                    {
                        extend: 'copyHtml5',
                        text: 'Copy Content to Clipboard',
                        className: 'btn',
                    },
                    {
                        extend: 'excelHtml5',
                        text: 'XLS Download',
                        className: 'btn',
                        "mColumns": [ 8 ]
                    },
                    {
                        extend: 'csvHtml5',
                        text: 'CSV Download',
                        className: 'btn',
                        "mRender": function (data, type, row) {
                             console.log(data);
                        }

                    },
        ],

    });
});

I am using Datatable Buttons as tabletools is deprecated from data table and tried many options:

"mRender": function (data, type, row) {
                                 console.log(data);
                            }

Also tried:

"mColumns": [ 8 ]

I have tried applying the different parameters in various ways but there is something missing in my understanding.Kindly help.

Thanks.


Solution

  • Look for exportOptions.columns, exportOptions let you define a column-selector the same way as you would target specific columns for filtering and so on. Example, include only the third and fourth column in a PDF export :

    $('#example').DataTable( {
      dom: 'Bfrtip',
      buttons: [
        {
          extend: 'pdf',
          exportOptions: {
            columns: [ 2, 3 ]
          }
        }
      ]
    });
    

    demo -> https://jsfiddle.net/r9Lqbhz4/

    Other examples :

    exportOptions: {
       columns: ':visible' //visible rows
    }
    exportOptions: {
       columns: ['.export', '.important'] //by class selector
    }
    

    And so on, see the above mentioned column-selector page,

    As per comment: All visible columns except for one in particular :

    exportOptions: {
       columns: ':visible:not(:eq(2))' 
    }
    

    Will include all visible columns except column #2, the third column. Be aware that the eq() column index is based on visible column indexes.