on()
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:
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:
Name Type Optional 1 event
No Event to listen for. Multiple events can be listened for using a space separator and events can be namespaced, just like with
jQuery().on()
.2 callback
No Event callback handler. For the argument list passed in, please refer to the documentation for the event that you are using.
- 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.