select
Items (rows, columns or cells) have been selected.
Please note - this property requires the Select extension for DataTables.
Description
This event is triggered whenever items (rows, columns or cells) are selected in a DataTable, and provides information about which item(s) have been selected.
Please note that for performance reasons only a single event is triggered for each selection action. The result is that if multiple items are selected in a single action (shift click in the os
selection style for example) each item selected does not receive its own event, but rather the information about the selected items is conveyed in an array.
Additionally, as with all DataTables emitted events, this event is triggered with the dt
namespace. As such, to listen for this event, you must also use the dt
namespace by simply appending .dt
to your event name (this is done automatically when using on()
and one()
).
Type
function function( e, dt, type, indexes )
- Parameters:
Name Type Optional 1 e
No jQuery event object
2 dt
No DataTables API instance
3 type
No Items being selected. This can be
row
,column
orcell
.4 indexes
No The DataTables' indexes of the selected items. This can be used with the table selector methods (
rows()
for example). For further information about the item indexes used by DataTables, please refer torow().index()
,column().index()
andcell().index()
as appropriate.
Examples
Get data from rows whenever rows are selected:
var table = new DataTable('#myTable');
table.on('select', function (e, dt, type, indexes) {
if (type === 'row') {
var data = table
.rows(indexes)
.data()
.pluck('id');
// do something with the ID of the selected items
}
});
Add a custom class when an item is selected:
var table = new DataTable('#myTable');
table.on('select', function (e, dt, type, indexes) {
table[type](indexes)
.nodes()
.to$()
.addClass('custom-selected');
});