one()
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, matching the core jQuery event methods:
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.
Type
function one( event, callback )
- Description:
Add an event listener, for which the callback will be fired once only and then the event listener removed.
- 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
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.