Murugesan Murugesan - 3 months ago 22
AngularJS Question

Export (Print ,pdf ,excel ,copy) the selected columns in Angular Data Table

This is my code to export the columns,

exportOptions: {
columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,... ]
}


I ve More than 20 columns and user can select only 5 columns in the front end( via colVis plugin).

If the user select 5 columns that columns should be exported.How can i do this?

Answer

If you are just using colvis you can get the columns that are visible

exportOptions: {
   columns: ':visible'
}

This will simply jQuery the table and only use the columns that are visible in your browser.

If you want to use the same model as colvis uses you would have to do something a bit more fansy/hacky. This is how I did it in relation to also using responsive plugin:

exportOptions: {
   columns: exportColumns
}

function exportColumns(i, values, header){
   var colvis = $(header).closest(".dataTable").DataTable().columns().visible();
      if(colvis[i]){
         return values;
      }
    }
Comments