特性羅列如下:
可變長度分頁;動態過濾;多列排序,帶數據類型檢測功能;列寬度的智能處理;從多種數據源獲取數據(DOM,js Array, ajax file, server-side returning);滾動配置屬性;完整國際化支持; jquery UI ThemeRoller支持;經歷了個2600多個單元測試,相當牢固;有為數不少的插件支持;免費的;狀態保存能力;支持隱藏列;動態創建表格的能力;自動ajax數據加載;自定義DOM位置;單列過濾(這個與前面的過濾有區別么?);多種分頁器;無損的DOM交互;參與排序的列高亮;高級數據源配置;擴展的插件支持功能;可以用css完全配置表象;豐富的文檔; 110多個例子;對Adobe AIR的完全支持。
引入
使用
Javascript代碼
要注意的是,要被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前執行,可以方便地做一些預操作 |
下面是我在工作中的一個使用案例:
html代碼
<div class="modal-body" style="padding-bottom: 50px; padding-top: 30px;"> <table id="kpiTables" class="table table-hover"> <thead> <tr> <th>Select</th> <th>KPI</th> </tr> </thead> <tbody> </tbody> </table> </div>
變相的一種ajax獲取數據, 生成checkbox的方法
function getKpis(){ ajaxRequestOptions = { url : "/ajax/assign/kpis", type : "GET", data : null, contentType : 'application/x-www-form-urlencoded; charset=UTF-8', processData : true, async : false, cache : false, onSuccess : function(response, textStatus, jqXHR) { var kpis = response.results[0]; $('#kpiTables').DataTable({ "aaData" : kpis, aoColumns: [ { data: 'id', "bSortable": false, "mRender": function (data) { return '<input type="checkbox" class="check" value="'+data+'"/>'; } },{ "data" : "name" }], "aaSorting": [[1,'asc']] }); }, onFail : null, onAlways : null }; ajaxExecutor(ajaxRequestOptions); } var kpiIds = []; $('#kpiTables').on( 'click', '.check', function () { if($(this).prop("checked")){ kpiIds.push($(this).val()); }else{ for (var i = 0; i < kpiIds.length; i++) { if (kpiIds[i] == $(this).val()) { kpiIds = removeElement(i, kpiIds);// delete element } } } }); function removeElement(index, array) { if (index >= 0 && index < array.length) { for (var i = index; i < array.length; i++) { array[i] = array[i + 1]; } array.length = array.length - 1; } return array; }
在bootstrap-modal關閉的時候清空數據,否則在頁面沒有刷新的情況下再次打開模態框會報錯
$('body').on('hidden.bs.modal','.modal', function () { $(this).removeData("bs.modal"); kpiIds = []; if ($('#kpiTables').hasClass('dataTable')) { var dttable = $('#kpiTables').dataTable(); dttable.fnClearTable(); //clear table dttable.fnDestroy(); //destroy datatables $('#kpiTables thead').empty(); $('#kpiTables thead').append('<tr><th>Select</th><th>KPI</th></tr>'); } })
*摘自:http://daogangtang.iteye.com/blog/1107852