{hero}

選取

自:Select 1.0.0 起

項目 (列、欄或儲存格) 已被選取。
請注意 - 此屬性需要 DataTables 的 Select 擴充功能。

說明

每當在 DataTable 中選取項目 (列、欄或儲存格) 時,會觸發此事件,並提供有關已選取哪些項目的資訊。

請注意,為了效能考量,每個選取動作只會觸發單一事件。結果是,如果在單一動作中選取多個項目 (例如,在 os 選取樣式中按住 Shift 鍵點擊),每個選取的項目不會收到自己的事件,而是關於選取項目的資訊會以陣列的形式傳達。

此外,與所有 DataTables 發出的事件一樣,此事件會以 dt 命名空間觸發。因此,要監聽此事件,您還必須使用 dt 命名空間,只需將 .dt 附加到您的事件名稱 (當使用 on()one() 時,會自動完成此操作)。

類型

function function( e, dt, type, indexes )

參數

範例

每當選取列時,從列取得資料

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
	}
});

選取項目時新增自訂類別

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

table.on('select', function (e, dt, type, indexes) {
	table[type](indexes)
		.nodes()
		.to$()
		.addClass('custom-selected');
});