one()

Since: DataTables 1.10

Listen for a table event once and then remove the listener.

Description

DataTables can trigger a number of events which can be useful for taking action when DataTables performs those events. For example, it is often useful to know when an Ajax event occurs (xhr), so you can add additional data to the JSON payload.

DataTables provides three methods for working with DataTables events:

  • on() - Listen for events
  • off() - Stop listening for events
  • one() - Listen for a single event.

This one() method is used to listen for DataTables an event from DataTables and then immediately remove that event once it has fired for the first time. Simply pass in the event you wish to listen for and provide a callback function which will be activated, and then removed, when the event is triggered by DataTables.

Backwards compatibility note: if jQuery is loaded on your page, or registered with DataTables (DataTable.use()), DataTables will use jQuery for event handling. As of DataTables 3, if jQuery is not available, it will use its own event handling.

Type

one( event, callback )

Add an event listener, for which the callback will be fired once only and then the event listener removed.

Parameters:

Returns:

DataTable.Api: DataTables API instance

Example

Listen for only the first xhr event from DataTables:

var table = new DataTable('#myTable', {
	ajax: '/data',
	serverSide: true
});

table.one('xhr', function (e, settings, json) {
	console.log('Ajax event occurred. Returned data: ', json);
});

Related

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