{hero}

cell().index()

自:DataTables 1.10 起

取得選定儲存格的索引資訊。

說明

DataTables 將列和行的資料儲存在內部索引中,以便快速排序、搜尋等。 有時候知道這些索引很有用,因為它們可以用於 row()column() 以及其他使用選取器的 API 方法中的有效選取器。

方便的是,此方法還提供了可見的列索引以及列資料索引,因為可以從文件中動態添加和刪除列。

cell() 選取的結果集中,針對儲存格傳回的資料結構為

{
    "row":           integer, // Row index
    "column":        integer, // Column data index 
    "columnVisible": integer  // Column visible index
}

類型

function cell().index()

說明

取得列、行和可見行索引資訊

傳回值

包含選定儲存格索引資訊的物件。有關物件結構,請參閱下文。

範例

警示點擊儲存格的可見行索引 (計數)

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

table.on('click', 'tbody td', function () {
	alert(
		'Clicked on cell in visible column: ' +
			table.cell(this).index().columnVisible
	);
});

在列選取器中使用列索引,以將類別新增至點擊的列

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

table.on('click', 'tbody td', function () {
	var rowIdx = table.cell(this).index().row;

	table
		.rows(rowIdx)
		.nodes()
		.to$()
		.addClass('clicked');
});

相關

以下選項直接相關,並且在您的應用程式開發中可能也很有用。