count()

Since: DataTables 1.10.8

Get the number of elements that are contained in an API instance.

Description

If can sometimes be useful to know how many items are contained in an API's result set - a function that is provided by this method.

It is important to note that the length property cannot simply be reliably used itself to determine if the API result set contains any elements or not as the DataTables API is multi-table aware and the result set consists of nested arrays. Therefore we must flatten arrays before being able to use the length property. This method does exactly that (i.e. it is a shortcut for api.flatten().length).

Type

function count()

Description:

Get the number of entries in an API instance's result set, regardless of multi-table grouping (e.g. any data, selected rows, etc).

Returns:

The number of items in the API instance's result set

Examples

Count how many rows there are with the class selected:

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

alert( 'Rows '+table.rows( '.selected' ).count()+' are selected' );

Count the number of data entries (i.e. rows) in a table:

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

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

Related

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