each()
Iterate over the contents of the API result set.
Description
This method makes use of the fact that DataTables API objects are "array like", in that they inherit a lot of the abilities and methods of the Javascript Array
type.
Note that when working with the plural methods such as rows()
and columns()
you may wish to use the rows().every()
, columns().every()
and cells().every()
methods to iterate over each row, column or cell with the context set to that table element. This might sound a little complicated, but it can significantly simplify your code! Please refer to the documentation for each of the every
methods for full details.
The each()
method is a proxy for the Javascript Array.prototype.forEach
method and is provided as a utility method for the DataTables API. For more information about the original method, please refer to the Mozilla MDN documentation for forEach
. In browsers which do not support forEach
natively, a polyfill is provided to allow this DataTables method to operate as expected.
Type
function each( fn )
- Description:
Iterate over the contents of the API result set.
- Parameters:
Name Type Optional 1 fn
No Callback function which is called for each item in the API instance result set. The callback is called with three parameters:
- The element value
- The element index in the result set
- The API instance being traversed
No return value is expected.
- Returns:
Original API instance that was used. For chaining.
Example
Loop over the data from a column:
var table = new DataTable('#myTable');
table
.column(0)
.data()
.each(function (value, index) {
console.log('Data in index: ' + index + ' is: ' + value);
});