Set the data source for the column from the rows data object / array.
This property can be used to read and write data to and from any data source property, including deeply nested objects / properties. data can be given in a number of different ways which affect its behaviour as documented below.
The data that is resolved for a data point (between this option and columns.render ) will be used by DataTables for the requested data, with three special cases:
As of DataTables 2, the resolved data can be a DOM node. This is useful for cases when you are working with external frameworks which generate DOM nodes themselves. In such cases, don't use DataTables columns.render option to customise the content of the cells - rather do it with whatever is creating the cells in the first place.
Note that data is both a getter and setter option. If you just require formatting of data for output, you will likely want to use columns.render which is simply a getter and thus much simpler to use!
As of DataTables 1.10.3 this option can be used with a DOM sourced data to instruct DataTables where to write the data read for each column to in a data source object. By default DataTables will store the data in an array, but using this option you can provide object property names which describe the structure of the object to use (example).
Treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).
Read and write an object property to and from the data source. There are three 'special' options that can be used in the string to alter how DataTables reads the data from the source object:
Use the original data source for the row rather than plucking data directly from it. This action has effects on two other initialisation options:
Use different data for the different data types requested by DataTables ( filter , display , type or sort ). The property names of the object is the data type the property refers to and the value can defined using an integer, string or function using the same rules as columns.data normally does. Note that an _ option must be specified. This is the default value to use if you haven't specified a value for the data type requested by DataTables.
As an example you might use:
"data":
The function given will be executed whenever DataTables needs to set or get the data for a cell in the column.
This function might be called multiple times, as DataTables will call it for the different data types that it needs - sorting, filtering and display.
Please note that DataTables will call the function as a setter when a new row is added only when the row's data is read from the DOM (i.e. the table is initialised on a pre-populated HTML table). The function is not called as setter when the data is source from Javascript or Ajax under the assumption that the data is already in the format required.
The data for the whole row
The data type requested for the cell. Predefined types are filter , display , type or sort .
Value to set if the type parameter is set . Otherwise, undefined .
Since 1.10.1: An object that contains additional information about the cell being requested. This object contains the following properties:
The return value from the function is not required when 'set' is the type of call, but otherwise the return is what will be used for the data requested.
Takes the index value of the column automatically
Read table data from objects:
// JSON structure for each row in this example: // < // "engine": , // "browser": , // "platform": , // "version": , // "grade": // > new DataTable('#myTable', < ajaxSource: 'sources/objects.txt', columns: [ < data: 'engine' >, < data: 'browser' >, < data: 'platform' >, < data: 'version' >, < data: 'grade' >] >);
Read information from deeply nested objects:
// JSON structure for each row: // < // "engine": , // "browser": , // "platform": < // "inner": // >, // "details": [ // , // ] // > new DataTable('#myTable', < ajaxSource: 'sources/deep.txt', columns: [ < data: 'engine' >, < data: 'browser' >, < data: 'platform.inner' >, < data: 'details.0' >, < data: 'details.1' >] >);
Read a DOM sourced table into data objects:
$(document).ready(function () < $('#example').DataTable(< columns: [ < data: 'name' >, < data: 'position' >, < data: 'office' >, < data: 'age' >, < data: 'start_date' >, < data: 'salary' >] >); >);
Using data as a function to provide different information for sorting, filtering and display. In this case, currency (price):
new DataTable('#myTable', < columnDefs: [ < targets: 0, data: function (row, type, val, meta) < if (type === 'set') < row.price = val; // Store the computed display and filter values for efficiency row.price_display = val == '' ? '' : '$' + numberFormat(val); row.price_filter = val == '' ? '' : '$' + numberFormat(val) + ' ' + val; return; >else if (type === 'display') < return row.price_display; >else if (type === 'filter') < return row.price_filter; >// 'sort', 'type' and undefined all just use the integer return row.price; > > ] >);
Using default content:
new DataTable('#myTable', < columnDefs: [ < targets: [0], data: null, defaultContent: 'Click to edit' >] >);
Using array notation - outputting a list from an array:
new DataTable('#myTable', < columnDefs: [ < targets: [0], data: 'name[, ]' >] >);
The following options are directly related and may also be useful in your application development.