off()
Table events removal.
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 off()
method is used to remove a listener that has already been attached to a DataTable. Simply pass in the event you wish to remove the listener for, and optionally the specific function if you want to remove only a single event listener.
Type
function off( event [, callback] )
- Description:
Remove event listeners that have previously been added with
on()
.- Parameters:
Name Type Optional 1 event
No Event name to remove. Multiple events can be removed by using a space separator or namespacing, just as with
jQuery().off()
.2 callback
Yes - default: Specific callback function to remove if you want to unbind a single event listener. If not given, all events which match the event name / namespace given in the first parameter will be removed.
- Returns:
DataTables API instance
Example
Listen for only the first xhr
event from DataTables:
var table = new DataTable('#myTable', {
ajax: '/data',
serverSide: true
});
table.on('xhr', function (e, settings, json) {
table.off('xhr');
console.log('Ajax event occurred. Returned data: ', json);
});
// note that this is the same effect as using `table.one(...);`
Related
The following options are directly related and may also be useful in your application development.