0
votes

I am using datatables and it allows the user to export the data as csv. My problem is how can I export the column headers only in data tables? I have searched the internet but can't find any reliable source for this one. Here's my code in exporting my data table in csv format:

/**
         * Datatable
         */
        SML.table = SML.$tbl.DataTable({
            "dom": 'Bfrtip',
            "buttons": [
                {
                extend: 'csv',
                charset: 'UTF-16LE',
                fieldSeparator: ',',
                bom: true,
                exportOptions: {
                        columns: [1,2,3,4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,44 ],
                    }
                }
            ],
            "order": [],
            "processing": true,
            "select": true,
            "iDisplayLength": 25,
            "searching": true,
            "bLengthChange" : true,
            "orderClasses": false,
            "language": {
                "url": "https://cdn.datatables.net/plug-ins/1.10.15/i18n/Japanese.json",
            },
            "columnDefs": [
                {
                "targets"  : [0,7],
                "orderable": false,
                },
                {
                    "targets": [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,44 ],
                    //if you can suggest some way to shorten this too
                    "visible": false,
                    "searchable": false
                },
            ],
            "drawCallback": function( settings ) {
              SML.$chk_all.iCheck('uncheck');
            },
        });

So my output should have two buttons which is Export data as csv and export csv template.

1

1 Answers

0
votes

Just got it! I just get the class of my table row and used it in excluding:

"buttons": [
  {
    extend: 'csv',
    text : 'Export Template',
    charset: 'UTF-16LE',
    bom: true,
    exportOptions : {
        columns: [1,2,3,4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,44 ],
        rows: ':not([role=row])'
    },
  },
]

Hope that this will help someone in the future.