{hero}

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.

As of Buttons 1.5.0 the exported data from this method will automatically attempt to determine if any rows in the table are selected. If they are, the export will be restricted to those rows. If no rows are selected, the full data set will be exported.

If this behaviour is not what you desire, set the selected option of the modifier object to be null. It will then include all rows in the export, regardless of any rows being selected. Equally, if you want to force the export to include only selected rows, even if no rows are selected (i.e. none would be exported), set this parameter to be true.

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 = new DataTable('#myTable');

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

Export only selected rows (with the Select extension):

var table = new DataTable('#myTable');

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

Get the data for the visible columns only:

var table = new DataTable('#myTable');

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

Format the header cells - adding the column index:

var table = new DataTable('#myTable');

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