buttons.exportData()

Since: Buttons 1.0.0

Obtain data from the DataTable that is suitable for exporting. Please note - this property requires the Buttons extension for DataTables.

Description

It is relatively common to wish to use Buttons to obtain the data from a DataTable so it can be exported in some form (copy to clipboard, save to Excel, etc). As this operation can be required by a number of plug-in button types, Buttons provides this method for DataTables to quickly and easily obtain data from a DataTable in a form that is suitable for export.

In essence this method will return an object that contains header, footer and body information from the table. It is then up to the caller to decide how to export that data - for example with a CSV file you would join the items for each row using commas and then join the rows with new lines.

Type

function buttons.exportData( [ options ] )

Description:

Obtain data from a DataTable that is suitable for exporting by saving into a file or copying to clipboard.

Parameters:
Returns:

An object which has three parameters:

  • header - An array of header data for the selected columns
  • footer - An array of footer data for the selected columns
  • body - An array of arrays, which each inner array representing a row, and its items being the cells for the selected columns.

Examples

Get all data in the table for export:

var table = $('#myTable').DataTable();

var data = table.buttons.exportData();
// Do something with the 'data' variable

Export only selected rows (with the Select extension):

var table = $('#myTable').DataTable();

var data = table.buttons.exportData( {
	modifier: {
		selected: true
	}
} );
// Do something with the 'data' variable

Get the data for the visible columns only:

var table = $('#myTable').DataTable();

var data = table.buttons.exportData( {
	columns: ':visible'
} );
// Do something with the 'data' variable

Format the header cells - adding the column index:

var table = $('#myTable').DataTable();

var data = table.buttons.exportData( {
	format: {
		header: function ( data, columnIdx ) {
			return columnIdx +': '+ data;
		}
	}
} );
// Do something with the 'data' variable