Search code examples
jquerydatatabledatatablestabletools

Exclude column from export in Datatables Tabletools


I have the following initialization for my datatables:

$(document).ready(function() {
    $('.datatable').dataTable({
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
            "sSwfPath": "/js/plugins/dataTables/swf/copy_csv_xls_pdf.swf",
        },
        "columnDefs": [{
            "targets": 'no-sort',
            "orderable": false
        }]

    });
});

The TableTools plugin for DT gives me the flash buttons for export. I would like to exclude table columns of a certain class .no-export. I am aware of the mColumns option but I cannot get it to apply for a column by class. I have read the answer here Excluding last column of jQuery DataTables.net TableTools where Joe Jonston suggest the possibility by setting:

"aoColumnDefs": [{ "mColumns": false, "aTargets": ["no-export"] }],

I have tried applying the code in various ways but there is something missing in my understanding.


Solution

  • SOLUTION

    See this answer for a possible solution.

    You can use mColumns to define a function that will return indexes of all columns having class no-export assigned to th element in the header.

    $('.datatable').dataTable({
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
            "sSwfPath": "/js/plugins/dataTables/swf/copy_csv_xls_pdf.swf",
            "aButtons": [{
              "sExtends": "csv",
              "mColumns": function (settings) {
                   var api = new $.fn.dataTable.Api( settings );
                   return api.columns(":not(.no-export)").indexes().toArray();
              }
            }]
        }
    });