{hero}

on()

Since: DataTables 1.10

Table events 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, matching the core jQuery event methods:

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

This on() method is used to start listening for DataTables events. Simply pass in the event you wish to listen for an provide a callback function which will be activated when the event is triggered by DataTables.

Type

function on( event, callback )

Description:

Listen for events from tables and fire a callback when they occur

Parameters:
Returns:

DataTables API instance

Example

Log a console message on each xhr event:

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

table.on('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.