1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
<?xml version="1.0" encoding="UTF-8" ?>
<dt-api group="cells">
<name>cells().every()</name>
<summary>Iterate over each selected cell, with the function context set to be the cell in question.</summary>
<since>1.10.6</since>
<type type="function">
<signature>cells().every( fn )</signature>
<description>Iterate over each selected cell</description>
<parameter type="function" name="fn">
Function to execute for every cell selected. The function's content is set to be an API instance for the cell in question.
As of DataTables 1.10.8 the function is passed the following parameters:
1. Cell's row index
2. Cell's column index
3. Table loop counter
4. Cell loop counter
No return value is expected or acted upon.
</parameter>
<returns type="DataTables.Api">DataTables API instance of the selected cells.</returns>
</type>
<description>
An often used operation with the DataTable API is to perform an operation on a collection of cells - a common action is performed on each cell, such as adding event handlers, updating data, etc. This iteration of the cells can be performed a number of ways in DataTables, each with its own advantages:
* `dt-api cells().every()`
* `dt-api iterator()`
* `dt-api each()`
This `dt-api cells().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 `dt-api cell()` instance for the cell 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 `dt-api cell().data()` available as `this.data()` in the callback given to this method.
Consider the following example using `dt-api each()`, which iterates over the cell indexes that have been selected - we are required to get the `dt-api cell()` object for each cell to be able to work with it directly:
```js
table.cells().eq(0).each( function ( index ) {
var cell = table.cell( index );
var data = cell.data();
// ... do something with data(), or cell.node(), etc
} );
```
Using `dt-api cells().every()` this can be rewritten as:
```js
table.cells().every( function () {
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.
The other advantage is that the table context is automatically handled - in the first example above where `dt-api each()` is used, the `dt-api 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 `dt-api cells().every()` the table context is automatically set to the appropriate table for each cell that has been selected.
</description>
<example title="Add a class to cells which meet a logical requirement in the source data"><![CDATA[
var table = $('#example').DataTable();
table.cells().every( function () {
if ( this.data() > 50 ) {
$(this.node()).addClass( 'warning' );
}
else {
$(this.node()).removeClass( 'warning' );
}
} );
]]></example>
<related type="api">columns().every()</related>
<related type="api">each()</related>
<related type="api">iterator()</related>
<related type="api">rows().every()</related>
</dt-api>
|