File: rows%28%29.every%28%29.xml

package info (click to toggle)
datatables.js 1.11.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 22,848 kB
  • sloc: javascript: 65,075; xml: 10,712; php: 4,741; sh: 544; makefile: 18
file content (100 lines) | stat: -rw-r--r-- 3,924 bytes parent folder | download | duplicates (4)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?xml version="1.0" encoding="UTF-8" ?>
<dt-api group="rows">
	<name>rows().every()</name>
	<summary>Iterate over each selected row, with the function context set to be the row in question.</summary>
	<since>1.10.6</since>

	<type type="function">
		<signature>rows().every( fn )</signature>
		<description>Iterate over each selected row</description>
		<parameter type="function" name="fn">
			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:

			1. Row index
			2. Table loop counter
			3. Row loop counter
			4. Undefined (present only for consistency)

			No return value is expected or acted upon.
		</parameter>
		<returns type="DataTables.Api">DataTables API instance of the selected rows.</returns>
	</type>

	<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:

		* `dt-api rows().every()`
		* `dt-api iterator()`
		* `dt-api each()`

		This `dt-api 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 `dt-api 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 `dt-api row().data()` available as `this.data()` in the callback given to this method.

		Consider the following example using `dt-api each()`, which iterates over the row indexes that have been selected - we are required to get the `dt-api row()` object for each row to be able to work with it directly:

		```js
		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 `dt-api rows().every()` this can be rewritten as:

		```js
		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.

		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 rows().every()` the table context is automatically set to the appropriate table for each row that has been selected.
	</description>

	<example title="Add a child row to all rows, passing in a jQuery created `dt-tag tr` element and show all child rows"><![CDATA[
var table = $('#example').DataTable();

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();
} );

]]></example>

	<example title="Update all rows in the table, redrawing only when complete"><![CDATA[
var table = $('#example').DataTable();

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();

]]></example>


	<related type="api">columns().every()</related>
	<related type="api">cells().every()</related>
	<related type="api">each()</related>
	<related type="api">iterator()</related>
</dt-api>