JQuery之DataTables強大的表格解決方案
阿新 • • 發佈:2019-01-30
1、DataTables的預設配置
$(document).ready(function() { $(‘#example’).dataTable(); } );
2、DataTables的一些基礎屬性配置
“bPaginate”: true, //翻頁功能 “bLengthChange”: true, //改變每頁顯示資料數量 “bFilter”: true, //過濾功能 “bSort”: false, //排序功能 “bInfo”: true,//頁尾資訊 “bAutoWidth”: true//自動寬度
3、資料排序
$(document).ready(function() { $(‘#example’).dataTable( { “aaSorting”: [ [4, "desc" ] ] } ); } ); 從第0列開始,以第4列倒序排列
4、多列排序
5、隱藏某些列
$(document).ready(function() { $(‘#example’).dataTable( { “aoColumnDefs”: [ { "bSearchable": false, "bVisible": false, "aTargets": [ 2 ] }, { “bVisible”: false, “aTargets”: [ 3 ] } ] } ); } );
6、改變頁面上元素的位置
$(document).ready(function() { $(‘#example’).dataTable( { “sDom”: ‘<”top”fli>rt<”bottom”p><”clear”>’ } ); } ); //l- 每頁顯示數量 //f – 過濾輸入 //t – 表單Table //i – 資訊 //p – 翻頁 //r – pRocessing //< and > – div elements //<”class” and > – div with a class //Examples: <”wrapper”flipt>, <lf<t>ip>
7、狀態儲存,使用了翻頁或者改變了每頁顯示資料數量,會儲存在cookie中,下回訪問時會顯示上一次關閉頁面時的內容。
$(document).ready(function() { $(‘#example’).dataTable( { “bStateSave”: true } ); } );
8、顯示數字的翻頁樣式
$(document).ready(function() { $(‘#example’).dataTable( { “sPaginationType”: “full_numbers” } ); } );
9、水平限制寬度
$(document).ready(function() { $(‘#example’).dataTable( { “sScrollX”: “100%”, “sScrollXInner”: “110%”, “bScrollCollapse”: true } ); } );
10、垂直限制高度
11、水平和垂直兩個方向共同限制
12、改變語言
$(document).ready(function() { $(‘#example’).dataTable( { “oLanguage”: { “sLengthMenu”: “每頁顯示 _MENU_ 條記錄”, “sZeroRecords”: “抱歉, 沒有找到”, “sInfo”: “從 _START_ 到 _END_ /共 _TOTAL_ 條資料”, “sInfoEmpty”: “沒有資料”, “sInfoFiltered”: “(從 _MAX_ 條資料中檢索)”, “oPaginate”: { “sFirst”: “首頁”, “sPrevious”: “前一頁”, “sNext”: “後一頁”, “sLast”: “尾頁” }, “sZeroRecords”: “沒有檢索到資料”, “sProcessing”: “<img src=\'#\'" /loading.gif’ />” } } ); } );
13、click事件
14/配合使用tooltip外掛
15、定義每頁顯示資料數量
$(document).ready(function() { $(‘#example’).dataTable( { “aLengthMenu”: [[10, 25, 50, -1], [10, 25, 50, "All"]] } ); } );
16、row callback
最後一列的值如果為“A”則加粗顯示
17、排序控制
$(document).ready(function() { $(‘#example’).dataTable( { “aoColumns”: [ null, { "asSorting": [ "asc" ] }, { “asSorting”: [ "desc", "asc", "asc" ] }, { “asSorting”: [ ] }, { “asSorting”: [ ] } ] } ); } );
18、從配置檔案中讀取語言包
$(document).ready(function() { $(‘#example’).dataTable( { “oLanguage”: { “sUrl”: “cn.txt” } } ); } );
19、是用ajax源
$(document).ready(function() { $(‘#example’).dataTable( { “bProcessing”: true, “sAjaxSource”: ‘./arrays.txt’ } ); } );
20、使用ajax,在伺服器端整理資料
$(document).ready(function() { $(‘#example’).dataTable( { “bProcessing”: true, “bServerSide”: true, “sPaginationType”: “full_numbers”, “sAjaxSource”: “./server_processing.php”, /*如果加上下面這段內容,則使用post方式傳遞資料 “fnServerData”: function ( sSource, aoData, fnCallback ) { $.ajax( { “dataType”: ‘json’, “type”: “POST”, “url”: sSource, “data”: aoData, “success”: fnCallback } ); }*/ “oLanguage”: { “sUrl”: “cn.txt” }, “aoColumns”: [ { "sName": "platform" }, { "sName": "version" }, { "sName": "engine" }, { "sName": "browser" }, { "sName": "grade" } ]//$_GET['sColumns']將接收到aoColumns傳遞資料 } ); } );
21、為每行載入id和class
伺服器端返回資料的格式: { “DT_RowId”: “row_8″, “DT_RowClass”: “gradeA”, “0″: “Gecko”, “1″: “Firefox 1.5″, “2″: “Win 98+ / OSX.2+”, “3″: “1.8″, “4″: “A” },
22、為每行顯示細節,點選行開頭的“+”號展開細節顯示
23、選擇多行
22、整合jQuery外掛jEditable
更過參考:
要注意的是,要被dataTable處理的table物件,必須有thead與tbody,而且,結構要規整(資料不一定要完整),這樣才能正確處理。
以下是在進行dataTable繫結處理時候可以附加的引數:
屬性名稱 | 取值範圍 | 解釋 |
bAutoWidth | true or false, default true | 是否自動計算表格各列寬度 |
bDeferRender | true or false, default false | 用於渲染的一個引數 |
bFilter | true or false, default true | 開關,是否啟用客戶端過濾功能 |
bInfo | true or false, default true | 開關,是否顯示錶格的一些資訊 |
bJQueryUI | true or false, default false | 是否使用jquery ui themeroller的風格 |
bLengthChange | true or false, default true | 開關,是否顯示一個每頁長度的選擇條(需要分頁器支援) |
bPaginate | true or false, default true | 開關,是否顯示(使用)分頁器 |
bProcessing | true or false, defualt false | 開關,以指定當正在處理資料的時候,是否顯示“正在處理”這個提示資訊 |
bScrollInfinite | true or false, default false | 開關,以指定是否無限滾動(與sScrollY配合使用),在大資料量的時候很有用。當這個標誌為true的時候,分頁器就預設關閉 |
bSort | true or false, default true | 開關,是否讓各列具有按列排序功能 |
bSortClasses | true or false, default true | 開關,指定當當前列在排序時,是否增加classes 'sorting_1', 'sorting_2' and 'sorting_3',開啟後,在處理大資料時,效能有所損失 |
bStateSave | true or false, default false | 開關,是否開啟客戶端狀態記錄功能。這個資料是記錄在cookies中的,打開了這個記錄後,即使重新整理一次頁面,或重新開啟瀏覽器,之前的狀態都是儲存下來的 |
sScrollX | 'disabled' or '100%' 類似的字串 | 是否開啟水平滾動,以及指定滾動區域大小 |
sScrollY | 'disabled' or '200px' 類似的字串 | 是否開啟垂直滾動,以及指定滾動區域大小 |
-- | -- | -- |
選項 | ||
aaSorting | array array[int,string], 如[], [[0,'asc'], [0,'desc']] | 指定按多列資料排序的依據 |
aaSortingFixed | 同上 | 同上。唯一不同點是不能被使用者的自定義配置衝突 |
aLengthMenu | default [10, 25, 50, 100],可以為一維陣列,也可為二維陣列,比如:[[10, 25, 50, -1], [10, 25, 50, "All"]] | 這個為選擇每頁的條目數,當使用一個二維陣列時,二維層面只能有兩個元素,第一個為顯示每頁條目數的選項,第二個是關於這些選項的解釋 |
aoSearchCols | default null, 類似:[null, {"sSearch": "My filter"}, null,{"sSearch": "^[0-9]", "bEscapeRegex": false}] | 給每個列單獨定義其初始化搜尋列表特性(這一塊還沒搞懂) |
asStripClasses | default ['odd', 'even'], 比如['strip1', 'strip2', 'strip3'] | 指定要被應用到各行的class風格,會自動迴圈 |
bDestroy | true or false, default false | 用於當要在同一個元素上執行新的dataTable繫結時,將之前的那個資料物件清除掉,換以新的物件設定 |
bRetrieve | true or false, default false | 用於指明當執行dataTable繫結時,是否返回DataTable物件 |
bScrollCollapse | true or false, default false | 指定適當的時候縮起滾動檢視 |
bSortCellsTop | true or false, default false | (未知的東東) |
iCookieDuration | 整數,預設7200,單位為秒 | 指定用於儲存客戶端資訊到cookie中的時間長度,超過這個時間後,自動過期 |
iDeferLoading | 整數,預設為null | 延遲載入,它的引數為要載入條目的數目,通常與bServerSide,sAjaxSource等配合使用 |
iDisplayLength | 整數,預設為10 | 用於指定一屏顯示的條數,需開啟分頁器 |
iDisplayStart | 整數,預設為0 | 用於指定從哪一條資料開始顯示到表格中去 |
iScrollLoadGap | 整數,預設為100 | 用於指定當DataTable設定為滾動時,最多可以一屏顯示多少條資料 |
oSearch | 預設{ "sSearch": "", "bRegex": false, "bSmart": true } | 又是初始時指定搜尋引數相關的,有點複雜,沒搞懂目前 |
sAjaxDataProp | 字串,default 'aaData' | 指定當從服務端獲取表格資料時,資料項使用的名字 |
sAjaxSource | URL字串,default null | 指定要從哪個URL獲取資料 |
sCookiePrefix | 字串,default 'SpryMedia_DataTables_' | 當開啟狀態儲存特性後,用於指定儲存在cookies中的字串的字首名字 |
sDom | default lfrtip (when bJQueryUI is false) or <"H"lfr>t<"F"ip> (when bJQueryUI is true) | 這是用於定義DataTable佈局的一個強大的屬性,另開專門文件來補充說明吧 |
sPaginationType | 'full_numbers' or 'two_button', default 'two_button' | 用於指定分頁器風格 |
sScrollXInner | string default 'disabled' | 又是水平滾動相關的,沒搞懂啥意思 |
DataTable支援如下回調函式
回撥函式名稱 | 引數 | 返回值 | 預設 | 功能 |
fnCookieCallback | 1.string: Name of the cookie defined by DataTables 2.object: Data to be stored in the cookie 3.string: Cookie expires string 4.string: Path of the cookie to set | string: cookie formatted string (which should be encoded by using encodeURIComponent()) | null | 當每次cookies改變時,會觸發這個函式呼叫 |
fnDrawCallback | 無 | 無 | 無 | 在每次table被draw完後呼叫,至於做什麼就看著辦吧 |
fnFooterCallback | 1.node : "TR" element for the footer 2.array array strings : Full table data (as derived from the original HTML) 3.int : Index for the current display starting point in the display array< 4.int : Index for the current display ending point in the display array 5.array int : Index array to translate the visual position to the full data array | 無 | 無 | 用於在每次重畫的時候修改表格的腳部 |
fnFormatNumber | 1.int : number to be formatted | String : formatted string for DataTables to show the number | 有預設的 | 用於在大數字上,自動加入一些逗號,分隔開 |
fnHeaderCallback | 1.node : "TR" element for the header 2.array array strings : Full table data (as derived from the original HTML) 3.int : Index for the current display starting point in the display array 4.int : Index for the current display ending point in the display array 5.array int : Index array to translate the visual position to the full data array | 無 | 無 | 用於在每次draw發生時,修改table的header |
fnInfoCallback | 1.object: DataTables settings object 2.int: Starting position in data for the draw 3.int: End position in data for the draw 4.int: Total number of rows in the table (regardless of filtering) 5.int: Total number of rows in the data set, after filtering 6.string: The string that DataTables has formatted using it's own rules | string: The string to be displayed in the information element. | 無 | 用於傳達table資訊 |
fnInitComplete | 1.object:oSettings - DataTables settings object | 無 | 無 | 表格初始化完成後呼叫 |
fnPreDrawCallback | 1.object:oSettings - DataTables settings object | Boolean | 無 | 用於在開始繪製之前呼叫,返回false的話,會阻止draw事件發生;返回其它值,draw可以順利執行 |
fnRowCallback | 1.node : "TR" element for the current row 2.array strings : Raw data array for this row (as derived from the original HTML) 3.int : The display index for the current table draw 4.int : The index of the data in the full list of rows (after filtering) | node : "TR" element for the current row | 無 | 當建立了行,但還未繪製到螢幕上的時候呼叫,通常用於改變行的class風格 |
fnServerData | 1.string: HTTP source to obtain the data from (i.e. sAjaxSource) 2.array objects: A key/value pair object containing the data to send to the server 3.function: Function to be called on completion of the data get process that will draw the data on the page. | void | $.getJSON | 用於替換預設發到服務端的請求操作 |
fnStateLoadCallback | 1.object:oSettings - DataTables settings object 2.object:oData - Object containing information retrieved from the state saving cookie which should be restored. For the exact properties please refer to the DataTables code. | Boolean - false if the state should not be loaded, true otherwise | 無 | 在cookies中的資料被載入前執行,可以方便地修改這些資料 |
fnStateSaveCallback | 1.object:oSettings - DataTables settings object 2.String:sValue - a JSON string (without the final closing brace) which should be stored in the state saving cookie. | String - the full string that should be used to save the state | 無 |
在狀態資料被儲存到cookies前執行,可以方便地做一些預操作 |