rows().every()
Iterate over each selected row, with the function context set to be the row in question.
Description
A typical operation with the DataTable API is to perform an operation on a collection of rows - a common action is performed on each row, adding event handlers, updating data, etc. This iteration of the rows can be performed a number of ways in DataTables, each with its own advantages:
This rows().every()
method is likely to be the most useful in the majority of cases as it sets the context of the callback function to be the row()
instance for the row in question (normally a callback in the DataTables API has its context set to be at the top level API hierarchy). In simple terms this means you have the methods such as row().data()
available as this.data()
in the callback given to this method.
Consider the following example using each()
, which iterates over the row indexes that have been selected - we are required to get the row()
object for each row to be able to work with it directly:
table.rows().eq(0).each( function ( index ) {
var row = table.row( index );
var data = row.data();
// ... do something with data(), or row.node(), etc
} );
Using rows().every()
this can be rewritten as:
table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
var data = this.data();
// ... do something with data(), or this.node(), etc
} );
Although a relatively simple optimisation in terms of code presentation, it can make the code much more readable and intuitive. It is also significantly more performant as of DataTables 2, as API instances do not need to create created every time around the loop.
The other advantage is that the table context is automatically handled - in the first example above where each()
is used, the eq()
method is used to select the information from the first table in the API's context only, introducing complexity if multiple tables are used. In rows().every()
the table context is automatically set to the appropriate table for each row that has been selected.
Type
function rows().every( fn )
- Description:
Iterate over each selected row
- Parameters:
Name Type Optional 1 fn
No Function to execute for every row selected. The function's content is set to be an API instance for the row in question.
As of DataTables 1.10.8 the function is passed the following parameters:
- Row index
- Table loop counter
- Row loop counter
No return value is expected or acted upon.
- Returns:
DataTables API instance of the selected rows.
Examples
Add a child row to all rows, passing in a jQuery created tr
element and show all child rows:
var table = new DataTable('#myTable');
table.rows().every(function (rowIdx, tableLoop, rowLoop) {
this.child(
$(
'<tr>' +
'<td>' +
rowIdx +
'.1</td>' +
'<td>' +
rowIdx +
'.2</td>' +
'<td>' +
rowIdx +
'.3</td>' +
'<td>' +
rowIdx +
'.4</td>' +
'</tr>'
)
).show();
});
Update all rows in the table, redrawing only when complete:
var table = new DataTable('#myTable');
table.rows().every(function (rowIdx, tableLoop, rowLoop) {
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.