Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables.Buttons hide columns in printing and exporting to excel

How can i hide the last column in printing and exporting to excel only but appear in the normal viewing

http://jsfiddle.net/andrew_safwat/qokbv1sj/

$(document).ready(function () {

            $('#example').DataTable({
                dom: 'Brtip',
                buttons: [
                    {
                        extend: 'print'
                    },
                    {
                        extend: 'excel'
                    }
                ]
            });
        });
like image 715
Andrew Safwat Avatar asked Nov 28 '15 14:11

Andrew Safwat


People also ask

How do I hide and show columns in DataTable?

To hide and show columns use columns() and visible() method. Call it on dataTables instance and pass column index in columns() method and false to visible() method. Similarly, pass true to visible() if you want to show the columns.

How do I hide the DataTable Export button?

You can use JQuery to disable/enable the button based on the row count. Here's an example where the CSV export is disabled if the row count is zero. You could tie this to rowCallback option or a custom event.


3 Answers

Use exportOptions Here's how you do it

$(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'
        ]
    } );
} );

https://datatables.net/extensions/buttons/examples/html5/columns.html

like image 137
Moses Machua Avatar answered Oct 13 '22 11:10

Moses Machua


$(document).ready(function () {
    $('#example').DataTable({
        dom: 'Brtip',
         buttons: [
            {
           extend: 'print',
           exportOptions: {
           columns: [ 0, 1, 2, 3, 4, 5, 6 ] //Your Column value those you want
               }
             },
             {
              extend: 'excel',
              exportOptions: {
              columns: [ 0, 1, 2, 3, 4 5, 6 ] //Your Column value those you want
             }
           },
         ],
    });
});
like image 24
Babasaheb Matsagar Avatar answered Oct 13 '22 10:10

Babasaheb Matsagar


Here is the answer

buttons.exportData(), you can specify which columns to export with a column-selector, which takes an array of IDs or Indexes

http://datatables.net/forums/discussion/comment/85649#Comment_85649

like image 43
Andrew Safwat Avatar answered Oct 13 '22 11:10

Andrew Safwat