Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables TableTools - Is it possible to only include the visible columns when saving CSV?

I did some tests using the TableTools plugin for Datatables on saving CSV.

I noticed that it saves everything. It does not care if you filtered the rows nor if you hide some columns.

Is there a way to only save the data from visible columns?

like image 901
developarvin Avatar asked May 06 '14 00:05

developarvin


2 Answers

You want to use the mColumns when defining your buttons.

An example from the documentation:

$(document).ready( function () {
    $('#example').dataTable( {
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "aButtons": [
                {
                    "sExtends": "csv",
                    "sButtonText": "Special columns",
                    "mColumns": [ 0, 1, 4 ]
                },
                {
                    "sExtends": "csv",
                    "sButtonText": "Visible columns",
                    "mColumns": "visible"
                }
            ]
        }
    } );
} );

This defines two buttons that will export to a CSV file. The first one, labeled Special columns, will only exports columns 0,1,4. The second button, labeled Visible columns will export all columns that are visible.

The mColumns parameter is described like this in the documentation:

The parameter can either be a string with a value of 'all', 'visible', 'hidden' or 'sortable' - or an array of integers with the column indexes to be exported.

This functionality is not new in the 1.10 version. It was also available in the older version, if you have no yet upgraded.

like image 96
Andy Avatar answered Nov 03 '22 01:11

Andy


Just for completion... on the last version of DataTables, it's slightly different (here's the documentation):

$(document).ready(function() {
  $('#example').DataTable( {
      dom: 'Bfrtip',
      buttons: [
          {
              extend: 'print',
              exportOptions: {
                  columns: ':visible'
              }
          },
          'colvis'
      ],
      columnDefs: [ {
          targets: -1,
          visible: false
      } ]
  } );
} );
like image 25
luthier Avatar answered Nov 02 '22 23:11

luthier