{hero}

columns().cache()

自:DataTables 1.10 起

取得所選欄位的 DataTables 快取資料。

已棄用!

自 v2.0 起,此功能已被棄用。此功能尚未排定移除時程,但不建議使用,應使用以下討論的替代方案。

使用 column().render() 來取得特定呈現類型的資料,而不是存取 DataTables 的快取。此方法將在 DataTables 3 中移除。

描述

DataTables 會快取搜尋和排序的資料,以便在需要時盡快執行這些操作。有時取得 DataTables 為這些操作快取的資料會很有用,例如在建立 select 清單以提供基於欄位的篩選器時。

無法保證快取資料在任何特定時刻都可用。如果 DataTables 沒有請求資料,則不會快取資料。當使用 order 選項且未對欄位執行排序時,這一點尤其明顯。資料的失效也會導致快取被移除。

請注意,返回陣列中資料的順序以及從哪些列取得資料(搜尋列、可見列等)是由用於取得所選欄位的 column() 選擇器的 selector-modifier 選項控制的。

請注意,此方法主要針對需要存取 DataTables 儲存的內部資料的外掛程式開發人員。

類型

function columns().cache( [ type ] )

描述

從選擇器取得欄位的資料

參數
返回值

DataTables API 實例,包含結果集中所選欄位中每個儲存格的資料。這是一個 2D 陣列,頂層陣列條目對應 columns() 選擇器所匹配的每個欄位。

範例

使用 select-filter 類別為每個欄位建立搜尋。

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

table.columns('.select-filter').every(function () {
	var that = this;

	// Create the select list and search operation
	var select = $('<select />')
		.appendTo(this.footer())
		.on('change', function () {
			that.search($(this).val()).draw();
		});

	// Get the search data for the first column and add to the select list
	this.cache('search')
		.sort()
		.unique()
		.each(function (d) {
			select.append($('<option value="' + d + '">' + d + '</option>'));
		});
});

相關

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