Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude column from export in jQuery Datatables

Tags:

datatables

I'm using jQuery datatable 1.10.11 and it's export button functionality as described here:

I want to skip last column from being export into excel file as this column has edit/delete buttons in it. My columns are generated dynamically so I can't use following method:

    $('#reservation').DataTable({
    dom: 'Bfrtip',
    buttons: [
        {
            extend: 'excel',
            text: 'Export Search Results',
            className: 'btn btn-default',
            exportOptions: {
                columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
            }
        }
    ]
});

I know this question is asked multiple time but non of them worked for me, might be version issue.

like image 559
Umair Malik Avatar asked Apr 21 '16 08:04

Umair Malik


4 Answers

Try using CSS selector that excludes last column for columns option.

$('#reservation').DataTable({
   dom: 'Bfrtip',
   buttons: [
      {
         extend: 'excel',
         text: 'Export Search Results',
         className: 'btn btn-default',
         exportOptions: {
            columns: 'th:not(:last-child)'
         }
      }
   ]
});
like image 84
Gyrocode.com Avatar answered Dec 17 '22 17:12

Gyrocode.com


You can add a class:

<th class='notexport'>yourColumn</th>

then exclude by class:

$('#reservation').DataTable({
   dom: 'Bfrtip',
   buttons: [
   {
        extend: 'excel',
        text: 'Export Search Results',
        className: 'btn btn-default',
        exportOptions: {
            columns: ':not(.notexport)'
        }
    }]
});
like image 20
Felipe Castilho Avatar answered Dec 17 '22 17:12

Felipe Castilho


I just thought I'd add this in because the accepted answer only works to exclude if you are not already including something else (such as visible columns).

In order to include only visible columns except for the last column, so that you can use this in conjunction with the Column Visibility Button, use

$('#reservation').DataTable({
    dom: 'Bfrtip',
    buttons: [
    {
        extend: 'excel',
        text: 'Export Search Results',
        className: 'btn btn-default',
        exportOptions: {
            columns: ':visible:not(:last-child)'
        }
    }]
});

And if you want to explicitly add your own class:

    $('#reservation').DataTable({
    dom: 'Bfrtip',
    buttons: [
    {
        extend: 'excel',
        text: 'Export Search Results',
        className: 'btn btn-default',
        exportOptions: {
            columns: ':visible:not(.notexport)'
        }
    }]
});    
like image 44
knsheely Avatar answered Dec 17 '22 16:12

knsheely


for Excel, csv, and pdf

dom: 'lBfrtip',
buttons: [
    {
        extend: 'excelHtml5',
        text: '<i class="fa fa-file-excel-o"></i> Excel',
        titleAttr: 'Export to Excel',
        title: 'Insurance Companies',
        exportOptions: {
            columns: ':not(:last-child)',
        }
    },
    {
        extend: 'csvHtml5',
        text: '<i class="fa fa-file-text-o"></i> CSV',
        titleAttr: 'CSV',
        title: 'Insurance Companies',
        exportOptions: {
            columns: ':not(:last-child)',
        }
    },
    {
        extend: 'pdfHtml5',
        text: '<i class="fa fa-file-pdf-o"></i> PDF',
        titleAttr: 'PDF',
        title: 'Insurance Companies',
        exportOptions: {
            columns: ':not(:last-child)',
        },
    },
]
like image 39
Tushar Saha Avatar answered Dec 17 '22 17:12

Tushar Saha