{hero}

trigger()

Since: DataTables 2.0

Trigger an event.

Description

This method is designed for use by DataTables extension authors to be able to trigger events in the same way that DataTables does internally. Events can be useful to let developers know that a certain action has happened and that they can perform some other operations. For example the Buttons extension triggers events when a button is processing an action or that an action has happened.

As with the other DataTables events, any event triggered with this method will have the .dt namespace appended to it (for jQuery name space handling). Additionally, event handlers are executed with the scope the table element for the DataTable and the event object has a dt property appended to it containing a DataTables API instance.

By default triggered events do not bubble up the document, as that operation has a performance impact. However, it is possible to enable bubbling with the optional third parameter. If enabled and the table is not in the document, the event will still be triggered on the body element.

Type

function trigger( name, args [, bubbles ] )

Description:

Trigger a DataTables related event.

Parameters:
Returns:

A result set that contains the returned values from the event handlers. This can be used to check for an event being cancelled with a false return (for example).

Examples

Listen for and trigger a custom event:

let table = new DataTable('#myTable');

table.on('customEvent', function (e, length, start) {
	console.log(length, start);
	// would print `1, 0`
});

table.trigger('customEvent', [1, 0]);

Access the DataTables API in a custom event:

let table = new DataTable('#myTable');

table.on('customEvent', function (e) {
	let info = e.dt.page.info();

	console.log(info);
});

table.trigger('customEvent');

Related

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