{hero}

any()

Since: DataTables 1.10.7

Determine if there are any entries in the result set.

Description

It can be useful to know if an API instance contains any data so you can determine what action to take. For example, knowing if the table has any data in it, if a row selector finds any rows or if specific data is available in the table.

While with a standard Javascript array you can simply test for the length property being 0, that isn't always true with the DataTables API object as it is multi-table aware. This means that it can contain arrays of information from multiple tables, which may themselves be empty.

This method provides a quick test to see if there are any results available in the API instance. Its result could also be determined by using the flatten() method and then checking the resulting length (i.e. api.flatten().length !== 0).

Type

function any()

Description:

Get a boolean value to indicate if there are any entries in the API instance's result set (i.e. any data, selected rows, etc).

Returns:

true if there there is one or more items in the result set, false otherwise.

Examples

Check if there are any rows with the class selected:

var table = $('#example').DataTable();

if ( table.rows( '.selected' ).any() ) {
	alert( 'Rows are selected' );
}

Find if the table has any data:

var table = $('#example').DataTable();

if ( ! table.data().any() ) {
	alert( 'Empty table' );
}

Related

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