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
|
<?xml version="1.0" encoding="UTF-8" ?>
<dt-api group="utility">
<name>filter()</name>
<summary>Create a new API instance with all elements from the result set which pass a given test.</summary>
<since>1.10</since>
<type type="function">
<signature>filter( fn )</signature>
<description>Iterate over the result set of an API instance and test each item, creating a new instance from those items which pass.</description>
<parameter type="function" name="fn">
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
The callback should return `true` if the value is to be included in the new instance's own result set, and false otherwise.
</parameter>
<returns type="DataTables.Api">New API instance with the values from the result set which passed the test in the callback.</returns>
</type>
<description>
The `dt-api filter()` method provides a way of filtering out content in an API instance's result set which does not pass the criteria set by the provided callback method. **This method should not be confused with `dt-api search()` which is used to search for records in the DataTable** - i.e. the filter method does not change the rows that are displayed in the DataTable.
When working with the plural methods such as `dt-api rows()` and `dt-api columns()` you may wish to use the `dt-api eq()` utility method to reduce the API instance from a 2D array to a 1D array which can be iterated over using this method.
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.
In this case, this method is a proxy for the Javascript `Array.prototype.filter` 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 `filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). In browsers which do not support `filter` natively, a polyfill is provided to allow this DataTables method to operate as expected.
</description>
<example title="Filter data from a column, to just the data that is greater than 20"><![CDATA[
var table = $('#example').DataTable();
var filteredData = table
.column( 0 )
.data()
.filter( function ( value, index ) {
return value > 20 ? true : false;
} );
]]></example>
<example title="Filter data from multiple columns, to just the data that is greater than 20 through use of the `dt-api eq()` method"><![CDATA[
var table = $('#example').DataTable();
var filteredData = table
.columns( [0, 1] )
.data()
.eq( 0 )
.filter( function ( value, index ) {
return value > 20 ? true : false;
} );
]]></example>
</dt-api>
|