{hero}

eq()

Since: DataTables 1.10

Reduce an Api instance to a single context and result set.

Description

The DataTables API is designed to work with multiple tables (contexts) in a single API instance, which can often be useful, but there times when you want to work with just a single table and benefit from the reduced complexity that invites (a scalar rather than vector).

The API's selectors methods will all provide their result sets in a 2D array format, with the outer array used to identify each table in the context, while the inner array contains that table's results from the selector. You can readily loop over each table if needed, but if you are using just a single table, this method can be used to reduce the API instance to just the table in question.

As an illustrative example, the rows() method will return the 2D array format described above, with the the inner array data containing the row indexes selected. For simple manipulation of a single table we want just a 1D array which contains the selected row indexes, and the API instance's context set to the table in question. This is exactly the ability the eq() method provides.

Please note that initially this method may appear to be similar to the get() method, but the two methods provide slightly different functionally:

  • eq() selects a specific DataTable from an API instance that references multiple tables
  • get() gets the data from the API instance.

Type

function eq( idx )

Description:

Reduce an API instance to a single context and result set, based on a given index. This can be useful for simplifying some interactions with the API.

Parameters:
Returns:

New DataTables API instance with the context and result set containing the table and data for the index specified. An empty API instance will be returned if there was not matching context.

Examples

Add a class to a row, based on a conditional expression:

var table = new DataTable('#myTable');

table
	.rows()
	.eq(0)
	.each(function (idx) {
		var row = table.row(idx);

		if (row.data().grade === 'A') {
			row
				.node()
				.to$()
				.addClass('gradeA');
		}
	});

Display the child rows for all rows in the table:

var table = new DataTable('#myTable');

table
	.rows()
	.eq(0)
	.each(function (idx) {
		table.row(idx).child.show();
	});

Related

The following options are directly related and may also be useful in your application development.