{hero}

stateSaveCallback

起始版本:DataTables 1.10

定義如何儲存及儲存表格狀態的回呼函數。

描述

DataTables 會儲存表格的狀態 (分頁、篩選等等),預設情況下會使用 HTML5 的 localStorage 來儲存狀態。當 stateSave 選項啟用時,此回呼方法允許您更改儲存狀態的位置 (例如,您可能希望使用伺服器端資料庫或 Cookie)。

傳遞給此函數的資料是一個具有以下結構的物件

{
    "time":   {number}               // Time stamp of when the object was created
    "start":  {number}               // Display start point
    "length": {number}               // Page length
    "order":  {array}                // 2D array of column ordering information (see `order` option)
    "search": {
        "search":          {string}  // Search term
        "regex":           {boolean} // Indicate if the search term should be treated as regex or not
        "smart":           {boolean} // Flag to enable DataTables smart search
        "caseInsensitive": {boolean} // Case insensitive flag
    },
    "columns" [
        {
            "visible": {boolean}     // Column visibility
            "search":  {}            // Object containing column search information. Same structure as `search` above
        }
    ]
}

請注意,額外的擴充套件可以將額外資訊新增至此結構,或者您可以使用 stateSaveParamsstateSaveParams 來新增您自己的參數。此外,儲存的資訊是區分資料類型的 — 也就是說,DataTables 提供的資料類型必須保留。例如,start 參數必須為 number 資料類型。

此方法僅用於儲存傳遞給它的資料。stateSaveParams 方法用於操作實際要儲存的資料。

此回呼函數與 stateLoadCallback 共同運作。此方法儲存狀態,而 stateSaveCallback 將從此回呼函數儲存的位置載入狀態。

類型

function stateSaveCallback( settings, data )

參數

範例

使用 Ajax 在伺服器上儲存狀態

new DataTable('#myTable', {
	stateSave: true,
	stateSaveCallback: function (settings, data) {
		// Send an Ajax request to the server with the state object
		$.ajax({
			url: '/state_save',
			data: data,
			dataType: 'json',
			type: 'POST',
			success: function () {}
		});
	}
});

相關

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