row().data()
Get / set the data for the selected row.
Description
This method is used to work with the data in the row retrieved by the row()
selector used. It can be used to get existing data, or set new data to be used for the row.
Note that when used as a setter, this method sets the data to apply to the table, but does not update the table's internal caches of data until the draw()
method is called. This can be done simply as a chained method of the row().data()
method's returned object - for example table.row( 0 ).data( newData ).draw();
. This is done to allow easy optimisation of the table where successive updates can be applied before the table is redrawn.
Types
function row().data()
- Description:
Get the data for the selected row
- Returns:
Data source object for the data source of the row. This will be an array if you use DOM sourced data, otherwise it will be the array / object / instance that is used to populate the table with data.
function row().data( d )
- Description:
Set the data for the selected row
- Parameters:
Name Type Optional 1 d
No Data to use for the row. This may be an array, object or Javascript object instance, but must be in the same format as the other data in the table (i.e. if your table uses objects, pass in an object here!).
- Returns:
DataTables API instance with the row retrieved by the selector in the result set.
Examples
Get the data for a single row when clicked upon:
var table = new DataTable('#myTable');
$('#example tbody').on('click', 'tr', function () {
console.log(table.row(this).data());
});
Increase a counter when a row is clicked on:
var table = new DataTable('#myTable');
$('#example tbody').on('click', 'tr', function () {
var d = table.row(this).data();
d.counter++;
table
.row(this)
.data(d)
.draw();
});
// Note that row().invalidate() could also be used for this example case
Update all rows in the table, redrawing only when complete:
var table = new DataTable('#myTable');
table.rows().every(function () {
var d = this.data();
d.counter++; // update data source for the row
this.invalidate(); // invalidate the data DataTables has cached for this row
});
// Draw once all updates are done
table.draw();
Related
The following options are directly related and may also be useful in your application development.