- var docrTable = $(‘#docrevisontable‘).dataTable({
- "bProcessing" : true, //DataTables載入數據時,是否顯示‘進度’提示
- "bServerSide" : true, //是否啓動服務器端數據導入
- "bStateSave" : true, //是否打開客戶端狀態記錄功能,此功能在ajax刷新紀錄的時候不會將個性化設定回覆爲初始化狀態
- "bJQueryUI" : true, //是否使用 jQury的UI theme
- "sScrollY" : 450, //DataTables的高
- "sScrollX" : 820, //DataTables的寬
- "aLengthMenu" : [20, 40, 60], //更改顯示記錄數選項
- "iDisplayLength" : 40, //默認顯示的記錄數
- "bAutoWidth" : false, //是否自適應寬度
- //"bScrollInfinite" : false, //是否啓動初始化滾動條
- "bScrollCollapse" : true, //是否開啓DataTables的高度自適應,當數據條數不夠分頁數據條數的時候,插件高度是否隨數據條數而改變
- "bPaginate" : true, //是否顯示(應用)分頁器
- "bInfo" : true, //是否顯示頁腳信息,DataTables插件左下角顯示記錄數
- "sPaginationType" : "full_numbers", //詳細分頁組,能夠支持直接跳轉到某頁
- "bSort" : true, //是否啓動各個字段的排序功能
- "aaSorting" : [[1, "asc"]], //默認的排序方式,第2列,升序排列
- "bFilter" : true, //是否啓動過濾、搜索功能
- "aoColumns" : [{
- "mDataProp" : "USERID",
- "sDefaultContent" : "", //此列默認值爲"",以防數據中沒有此值,DataTables加載數據的時候報錯
- "bVisible" : false //此列不顯示
- }, {
- "mDataProp" : "USERNAME",
- "sTitle" : "用戶名",
- "sDefaultContent" : "",
- "sClass" : "center"
- }, {
- "mDataProp" : "EMAIL",
- "sTitle" : "電子郵箱",
- "sDefaultContent" : "",
- "sClass" : "center"
- }, {
- "mDataProp" : "MOBILE",
- "sTitle" : "手機",
- "sDefaultContent" : "",
- "sClass" : "center"
- }, {
- "mDataProp" : "PHONE",
- "sTitle" : "座機",
- "sDefaultContent" : "",
- "sClass" : "center"
- }, {
- "mDataProp" : "NAME",
- "sTitle" : "姓名",
- "sDefaultContent" : "",
- "sClass" : "center"
- }, {
- "mDataProp" : "ISADMIN",
- "sTitle" : "用戶權限",
- "sDefaultContent" : "",
- "sClass" : "center"
- }],
- "oLanguage": { //國際化配置
- "sProcessing" : "正在獲取數據,請稍後...",
- "sLengthMenu" : "顯示 _MENU_ 條",
- "sZeroRecords" : "沒有您要搜索的內容",
- "sInfo" : "從 _START_ 到 _END_ 條記錄 總記錄數爲 _TOTAL_ 條",
- "sInfoEmpty" : "記錄數爲0",
- "sInfoFiltered" : "(所有記錄數 _MAX_ 條)",
- "sInfoPostFix" : "",
- "sSearch" : "搜索",
- "sUrl" : "",
- "oPaginate": {
- "sFirst" : "第一頁",
- "sPrevious" : "上一頁",
- "sNext" : "下一頁",
- "sLast" : "最後一頁"
- }
- },
- "fnRowCallback" : function(nRow, aData, iDisplayIndex) {
- /* 用來改寫用戶權限的 */
- if (aData.ISADMIN == ‘1‘)
- $(‘td:eq(5)‘, nRow).html(‘管理員‘);
- if (aData.ISADMIN == ‘2‘)
- $(‘td:eq(5)‘, nRow).html(‘資料下載‘);
- if (aData.ISADMIN == ‘3‘)
- $(‘td:eq(5)‘, nRow).html(‘通常用戶‘);
- return nRow;
- },
- "sAjaxSource" : "../use/userList.do?now=" + new Date().getTime(),
- //服務器端,數據回調處理
- "fnServerData" : function(sSource, aDataSet, fnCallback) {
- $.ajax({
- "dataType" : ‘json‘,
- "type" : "POST",
- "url" : sSource,
- "data" : aDataSet,
- "success" : fnCallback
- });
- }
- });
- $("#docrevisontable tbody").click(function(event) { //當點擊表格內某一條記錄的時候,會將此記錄的cId和cName寫入到隱藏域中
- $(docrTable.fnSettings().aoData).each(function() {
- $(this.nTr).removeClass(‘row_selected‘);
- });
- $(event.target.parentNode).addClass(‘row_selected‘);
- var aData = docrTable.fnGetData(event.target.parentNode);
- $("#userId").val(aData.USERID);
- $("#userN").val(aData.USERNAME);
- });
- $(‘#docrevisontable_filter‘).html(‘<span>用戶管理列表‘);
- $(‘#docrevisontable_filter‘).append(‘ <input type="button" class="addBtn" id="addBut" value="建立"/>‘);
- $(‘#docrevisontable_filter‘).append(‘ <input type="button" class="addBtn" id="addBut" value="修改"/>‘);
- $(‘#docrevisontable_filter‘).append(‘ <input type="button" class="addBtn" id="addBut" value="刪除"/>‘);
- $(‘#docrevisontable_filter‘).append(‘</span>‘);
- }
設置隱藏列
- var oTable = $(‘#example‘).dataTable();
- oTable.fnSetColumnVis( 0, false);//隱藏列
獲得當前頁面中的數據
$(document).ready(function() {
oTable = $(‘#example‘).dataTable();
oTable.$(‘tr‘).click( function () {
var data = oTable.fnGetData( this );
// ... do something with the array / object of data for the row
} );
} );
// Individual cell data
$(document).ready(function() {
oTable = $(‘#example‘).dataTable();
oTable.$(‘td‘).click( function () {
var sData = oTable.fnGetData( this );
alert( ‘The cell clicked on had the value of ‘+sData );
} );
} );
刷新表中的數據(最後一個參數是否重繪表格,false你瀏覽到第二頁不會刷跑到第一頁,但數據不會改變)
$(‘#example‘).dataTable().fnUpdate( [‘a‘,‘b‘] , 1 ); //row
獲得當前瀏覽的datatables頁碼
- var tableSetings=$(‘#example‘).dataTable().fnSettings()
- var paging_length=tableSetings._iDisplayLength;//當前每頁顯示多少
- var page_start=tableSetings._iDisplayStart;//當前頁開始
- var page=Div(page_start,paging_length);
- function Div(exp1, exp2) { //整除
- var n1 = Math.round(exp1); //四捨五入
- var n2 = Math.round(exp2); //四捨五入
- var rslt = n1 / n2; //除
- if (rslt >= 0) {
- rslt = Math.floor(rslt); //返回小於等於原rslt的最大整數。
- }
- else {
- rslt = Math.ceil(rslt); //返回大於等於原rslt的最小整數。
- }
- return rslt;
- }
設置datatables跳轉到某頁
- $(‘#example‘).dataTable().fnPageChange(page);
注意:因爲後臺數據可能已經被其它人改變(記錄個數與如今前臺不一致),因此數據fnUpdate時需要判斷先後端數據的一致性,只刷 新前臺有的數據的狀態
dom:
一:api
bAutoWidth :啓用或禁用自動列寬度的計算。
默認值 | true |
類型 | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bAutoWidth": false //關閉後,表格將不會自動計算表格大小,在瀏覽器大化小化的時候會擠在一坨
- } );
- } );
bDeferRender :根據官網的介紹翻譯過來就是,延期渲染,能夠有個速度的提高,當datatable 使用Ajax或者JS源表的數據。這個選項設置爲true,將致使datatable推遲建立表元素每一個元素,直到他們都建立完成——本參數的目的是節省大量的時間。
默認值: | false |
類型: | boolean |
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "sAjaxSource": "sources/arrays.txt",
- "bDeferRender": true //這個參數我我的沒有使用過,究竟是不是這個效果,你們本身去嘗試一下
- } );
- } );
bFilter :這個很容易明白,啓用或禁用過濾數據。在datatable過濾是「智能」,它容許用戶 最終輸入多個關鍵字(空格分隔),和每一行數據匹配,即便不是在被指定的順序(這容許匹配多個列)。注意,若是您但願使用過濾,在datatable中必須設置爲true,若是要刪除默認過濾輸入框和留住過濾功能,請使用{ @link DataTable.defaults.sDom }。這個最後一句不明白,不知道你們怎麼理解。
默認值: | true |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bFilter": false
- } );
- } );
bJQueryUI :這個不用多說了,一看就懂,啓用jQuery UI樣式,(須要在頁面添加jQuery的樣式文件)。
默認值: | false |
類型: | boolean |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bJQueryUI": true
- } );
- } );
bLengthChange :開啓一頁顯示多少條數據的下拉菜單,容許用戶從下拉框(十、2五、50和100),注意須要分頁(bPaginate:true)。
默認值: | true |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bLengthChange": false,
bPaginate :開啓分頁功能,若是不開啓,將會所有顯示
默認值: | true |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bPaginate": false
- } );
- } );
bProcessing :開啓讀取服務器數據時顯示正在加載中……特別是大數據量的時候,開啓此功能比較好
默認值: | false |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bProcessing": true
- } );
- } );
bScrollInfinite :是否開啓不限制長度的滾動條(和sScrollY屬性結合使用),不限制長度的滾動條意味着當用戶拖動滾動條的時候DataTable會不斷加載數據當數據集十分大的時候會有些用處,該選項沒法和分頁選項同時使用,分頁選項會被自動禁止,注意,額外推薦的滾動條會優先與該選項
默認值: | false |
類型: | boolean |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bScrollInfinite": true,
- "bScrollCollapse": true,
- "sScrollY": "200px"//長200像素
- } );
- } );
bServerSide :開啓服務器模式,使用服務器端處理配置datatable。注意:sAjaxSource參數也必須被給予爲了給datatable源代碼來獲取所需的數據對於每一個畫。 這個翻譯有點彆扭。開啓此模式後,你對datatables的每一個操做 每頁顯示多少條記錄、下一頁、上一頁、排序(表頭)、搜索,這些都會傳給服務器相應的值。
默認值: | false |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bServerSide": true,
- "sAjaxSource": "xhr.php"
- } );
- } );
bInfo :啓用或禁用表信息顯示。這顯示了關於數據的信息,目前在網頁上,包括信息過濾數據,若是行爲被執行。這個參數在bServerSide配置後須要用到。
默認值: | true |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bInfo": false//若是這個參數不穿到後臺去,服務器分頁會報錯,聽說這個參數包含了表的全部信息
- } );
- } );
bSort :開啓排序功能,每一列都有排序功能,若是關閉了,排序功能將失效,也能夠自定義排序功能。單個列的排序能夠禁用「bSortable」選項。
默認值: | true |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bSort": false
- } );
- } );
bSortClasses :是否在當前被排序的列上額外添加sorting_1,sorting_2,sorting_3三個class,當該列被排序的時候,能夠切換其背景顏色,該選項做爲一個來回切換的屬性會增長執行時間(當class被移除和添加的時候),當對大數據集進行排序的時候你或許但願關閉該選項
默認值: | true |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bSortClasses": false
- } );
- } );
bStateSave :是否開啓狀態保存,當選項開啓的時候會使用一個cookie保存表格展現的信息的狀態,例如分頁信息,展現長度,過濾和排序等。這樣當終端用戶從新加載這個頁面的時候可使用之前的設置
默認值: | false |
類型: | boolean |
- $(document).ready( function () {
- $(‘#example‘).dataTable( {
- "bStateSave": true
- } );
- } );
sScrollX :是否開啓水平滾動,當一個表格過於寬以致於沒法放入一個佈局的時候,或者表格有太多列的時候,你能夠開啓該選項從而在一個可橫向滾動的視圖裏面展現表格,該屬性能夠是css設置,或者一個數字(做爲像素量度來使用)
默認值: | blank string - i.e. disabled |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sScrollX": "100%",
- "bScrollCollapse": true
- } );
- } );
sScrollY:是否開啓垂直滾動,垂直滾動會驅使DataTable設置爲給定的長度,任何溢出到當前視圖以外的數據能夠經過垂直滾動進行察看當在小範圍區域中顯示大量數據的時候,能夠在分頁和垂直滾動中選擇一種方式,該屬性能夠是css設置,或者一個數字(做爲像素量度來使用)
默認值: | blank string - i.e. disabled |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sScrollY": "200px",
- "bPaginate": false
- } );
- } );
bDestroy :使用傳遞的新的初始化對象中的屬性構造一個新的表格,並替換一個匹配指定的選擇器的表格,若是沒有匹配到表格,新的表格會被做爲一個普通表格被構建
默認值: | false |
類型: | boolean |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sScrollY": "200px",
- "bPaginate": false
- } );
- // Some time later....
- $(‘#example‘).dataTable( {
- "bFilter": false,
- "bDestroy": true
- } );
- } );
bRetrieve :使用指定的選擇器檢索表格,注意,若是表格已經被初始化,該參數會直接返回已經被建立的對象,並不會顧及你傳遞進來的初始化參數對象的變化,將該參數設置爲true說明你確認已經明白這一點,若是你須要的話,bDestroy能夠用來從新初始化表格
默認值: | false |
類型: | boolean |
- $(document).ready( function() {
- initTable();
- tableActions();
- } );
- function initTable ()
- {
- return $(‘#example‘).dataTable( {
- "sScrollY": "200px",
- "bPaginate": false,
- "bRetrieve": true
- } );
- }
- function tableActions ()
- {
- var oTable = initTable();
- // perform API operations with oTable
- }
bScrollAutoCss :指明DataTable中滾動的標題元素是否被容許設置內邊距和外邊距等
默認值: | true |
類型: | boolean |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bScrollAutoCss": false,
- "sScrollY": "200px"
- } );
- } );
bScrollCollapse :當垂直滾動被容許的時候,DataTable會強制表格視圖在任什麼時候候都是給定的高度(對佈局有利)不過,當把數據集過濾到十分小的時候看起來會很古怪,並且頁腳會留在最下面當結果集的高度比給定的高度小時該參數會使表格高度自適應
默認值: | false |
類型: |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sScrollY": "200",
- "bScrollCollapse": true
- } );
- } );
bSortCellsTop :是否容許DataTable使用頂部(默認爲true)的單元格,或者底部(默認爲false)的單元格,當使用複合表頭的時候會有些用處
默認值: | false |
類型: | boolean |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bSortCellsTop": true
- } );
- } );
iCookieDuration :指定用於存儲客戶端信息到cookie中的時間長度,超過這個時間後,自動過時
默認值: | 7200 (2 hours) |
類型: | int |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "iCookieDuration": 60*60*24; // 一天
- } );
- } )
iDeferLoading :當選項被開啓的時候,DataTable在非加載第一次的時候不會向服務器請求數據,而是會使用頁面上的已有數據(不會應用排序等),所以在加載的時候保留一個XmlHttpRequest,iDeferLoading被用來指明須要延遲加載,並且也用來通知DataTable一個滿的表格有多少條數據,信息元素和分頁會被正確保留
默認值: | null |
類型: | int |
- // 57 records available in the table, no filtering applied
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bServerSide": true,
- "sAjaxSource": "scripts/server_processing.php",
- "iDeferLoading": 57
- } );
- } );
- // 57 records after filtering, 100 without filtering (an initial filter applied)
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bServerSide": true,
- "sAjaxSource": "scripts/server_processing.php",
- "iDeferLoading": [ 57, 100 ],
- "oSearch": {
- "sSearch": "my_filter"
- }
- } );
- } );
iDisplayLength :單頁顯示的數據的條數,若是bLengthChange屬性被開啓,終端用戶能夠經過一個彈出菜單重寫該數值
默認值: | 10 |
類型: | int |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "iDisplayLength": 50
- } );
- } )
iDisplayStart :當開啓分頁的時候,定義展現的記錄的起始序號,不是頁數,所以若是你每一個分頁有10條記錄並且想從第三頁開始,須要把該參數指定爲20
默認值 | 0 |
類型: | int |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "iDisplayStart": 20
- } );
- } )
iScrollLoadGap :滾動餘界是指DataTable在當前頁面還有多少條數據可供滾動時自動加載新的數據,你可能但願指定一個足夠大的餘界,以便滾動加載數據的操做對用戶來講是平滑的,同時也不會大到加載比須要的多的多的數據
默認值: | 100 |
類型: | int |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bScrollInfinite": true,
- "bScrollCollapse": true,
- "sScrollY": "200px",
- "iScrollLoadGap": 50
- } );
- } );
iTabIndex :默認狀況下DataTable容許經過爲須要鍵盤導航的元素添加tabindex屬性來進行導航(排序、分頁、過濾),容許你經過tab鍵切換控制組件,使用回車鍵去激活他們,默認爲0表示按照文檔流來切換,若是須要的話,你可使用該參數重寫切換順序,使用-1來禁止內建的鍵盤導航
默認值: | 0 |
類型: | int |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "iTabIndex": 1
- } );
- } );
oSearch :該參數容許你在初始化的時候使用已經定義的全局過濾狀態,sSearch對象必須被定義,可是全部的其它選項都是可選的,當bRegex爲true的時候,搜索字符串會被看成正則表達式,當爲false(默認)的時候,會被直接看成一個字符串,當bSmart爲true的時候,DataTable會使用使用靈活過濾策略(匹配任何可能的數據),爲false的時候不會這樣作
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "oSearch": {"sSearch": "Initial search"}
- } );
- } )
sAjaxDataProp :當使用Ajax數據源或者服務器端處理的時候,DataTable會默認搜索aaData屬性做爲數據源,該選項容許變動數據源的名稱,你可使用JavaScript的點號對象表示法去訪問多級網狀數據源
默認值: | aaData |
類型: | string |
- // Get data from { "data": [...] }
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "sAjaxSource": "sources/data.txt",
- "sAjaxDataProp": "data"
- } );
- } );
- // Get data from { "data": { "inner": [...] } }
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "sAjaxSource": "sources/data.txt",
- "sAjaxDataProp": "data.inner"
- } );
- } );
sAjaxSource :該參數用來向DataTable指定加載的外部數據源(若是想使用現有的數據,請使用aData),能夠簡單的提供一個能夠用來得到數據url或者JSON對象,該對象必須包含aaData,做爲表格的數據源
默認值: | null |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sAjaxSource": "list.action"
- } );
- } )
sCookiePrefix :該參數能夠用來重寫DataTable默認指定的用來存儲狀態信息的cookie的前綴
默認值: | SpryMedia_DataTables_ |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sCookiePrefix": "my_datatable_"
- } );
- } );
sDom :這是用於定義DataTable佈局的一個強大的屬性,包括分頁,顯示多少條數據和搜索,格式以下:
- The following options are allowed:
- ‘l‘ - 左上角按個下拉框,10個,20個,50個,全部的哪一個
- ‘f‘ - 快速過濾框
- ‘t‘ - 表格自己
- ‘i‘ - 分頁信息
- ‘p‘ -分頁按鈕
- ‘r‘ - 如今正在加載中……
- The following constants are allowed:
- ‘H‘ - jQueryUI theme "header" classes (‘fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix‘)
- ‘F‘ - jQueryUI theme "footer" classes (‘fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix‘)
- The following syntax is expected:
- ‘<‘ and ‘>‘ - div 元素
- ‘<"class" and ‘>‘ - 給div加clasa
- ‘<"#id" and ‘>‘ - 給div加上id
- Examples:
- ‘<"wrapper"flipt>‘
- ‘<lf<t>ip>‘
默認值: | lfrtip (when bJQueryUI is false) or <"H"lfr>t<"F"ip> (when bJQueryUI is true) |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sDom": ‘<"top"i>rt<"bottom"flp><"clear">‘
- } );
- } );
‘<"top"i>rt<"bottom"flp><"clear">‘
這段代碼翻譯爲html就是這樣子的:
- <div class="top">
- i
- </div>
- rt
- <div class="bottom">
- flp
- </div>
- <div class="clear"></div>
這樣一對比起來,就容易理解多了.Datatables之強大的sDom屬性的應用
sPaginationType :DataTable內建了兩種交互式分頁策略,兩個按鈕和全頁數,展示給終端用戶不一樣的控制方式,能夠經過API增長策略
默認值: | two_button |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sPaginationType": "full_numbers"
- } );
- } )
sScrollXInner :當橫向滾動可用的時候,該屬性能夠用來強制DataTable的寬度比須要的更長,好比你須要表格彼此相隔適宜,該變量能夠用來使表格變大,並且強制滾動,該該屬性能夠是css設置,或者一個數字(做爲像素量度來使用)
默認值: | blank string - i.e. disabled |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "sScrollX": "100%",
- "sScrollXInner": "110%"
- } );
- } );
sServerMethod :設置使用Ajax方式調用的服務器端的處理方法或者Ajax數據源的HTTP請求方式
默認值: | GET |
類型: | string |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "bServerSide": true,
- "sAjaxSource": "list.action",
- "sServerMethod": "POST" //以post的方式提交數據
- } );
- } )
aoColumnDefs:這個數組容許您針對一個特定列,多個列,或者全部列,使用aTargets屬性的數組中的每一個對象(請注意,介紹了aoColumnDefs datatable 1.7)。這提供了很大的靈活性在建立表,由於aoColumnDefs數組能夠是任意長度,目標是你特別想要的列。aTargets的屬性是一個數組來目標衆多列和每一個元素在它能夠:
- a string - class name will be matched on the TH for the column
- 0 or a positive integer - column index counting from the left
- a negative integer - column index counting from the right
- the string "_all" - all columns (i.e. assign a default)
aoColumns: If specified, then the length of this array must be equal to the number of columns in the original HTML table. Use ‘null‘ where you wish to use only the default values and automatically detected options.
aoColumnDefs參數和aoColumns能夠一塊兒使用,儘管aoColumnDefs優先aoColumns的靈活性。 若是二者都使用,aoColumns定義將最高優先級。一樣,若是相同的列的目標是在aoColumnDefs屢次,第一個元素的數組將最高優先級,最後一個最低的。
aDataSort:定義一個列或多個列自定義排序
Default: | null Takes the value of the column index automatically |
Type: | array |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "aDataSort": [ 0, 1 ], "aTargets": [ 0 ] },
- { "aDataSort": [ 1, 0 ], "aTargets": [ 1 ] },
- { "aDataSort": [ 2, 3, 4 ], "aTargets": [ 2 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "aDataSort": [ 0, 1 ] },
- { "aDataSort": [ 1, 0 ] },
- { "aDataSort": [ 2, 3, 4 ] },
- null,
- null
- ]
- } );
- } );
Default: | [ ‘asc‘, ‘desc‘ ] |
Type: | array |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "asSorting": [ "asc" ], "aTargets": [ 1 ] },
- { "asSorting": [ "desc", "asc", "asc" ], "aTargets": [ 2 ] },
- { "asSorting": [ "desc" ], "aTargets": [ 3 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- null,
- { "asSorting": [ "asc" ] },
- { "asSorting": [ "desc", "asc", "asc" ] },
- { "asSorting": [ "desc" ] },
- null
- ]
- } );
- } );
Default: | true |
Type: | boolean |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "bSearchable": false, "aTargets": [ 0 ] }
- ] } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "bSearchable": false },
- null,
- null,
- null,
- null
- ] } );
- } );
Default: | true |
Type: | boolean |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "bSortable": false, "aTargets": [ 0 ] }
- ] } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "bSortable": false },
- null,
- null,
- null,
- null
- ] } );
- } );
Default: | true |
Type: | boolean |
無例子代碼;
bVisible: 啓用或禁用本列顯示。
Default: | true |
Type: | boolean |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "bVisible": false, "aTargets": [ 0 ] }
- ] } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "bVisible": false },
- null,
- null,
- null,
- null
- ] } );
- } );
Default: | |
Type: | function |
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [3],
- "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
- if ( sData == "1.7" ) {
- $(nTd).css(‘color‘, ‘blue‘)
- }
- }
- } ]
- });
- } );
iDataSort:列索引(從0開始!),你想要執行一個在本專欄時被選中進行排序。這能夠用於排序在隱藏列例如。這個也沒使用過
Default: | -1 Use automatically calculated column index |
Type: | int |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "iDataSort": 1, "aTargets": [ 0 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "iDataSort": 1 },
- null,
- null,
- null,
- null
- ]
- } );
- } );
- integer - treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).
- string - read an object property from the data source. Note that you can use Javascript dotted notation to read deep properties / arrays from the data source.
- null - the sDefaultContent option will be used for the cell (null by default, so you will need to specify the default content you want - typically an empty string). This can be useful on generated columns such as edit / delete action columns.
- function - the function given will be executed whenever DataTables needs to set or get the data for a cell in the column. The function takes three parameters:
- {array|object} The data source for the row
- {string} The type call data requested - this will be ‘set‘ when setting data or ‘filter‘, ‘display‘, ‘type‘, ‘sort‘ or undefined when gathering data. Note that when undefined is given for the type DataTables expects to get the raw data for the object back
- {*} Data to set when the second parameter is ‘set‘.
Default: | null Use automatically calculated column index |
Type: | string |
- // Read table data from objects
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "sAjaxSource": "sources/deep.txt",
- "aoColumns": [
- { "mData": "engine" },
- { "mData": "browser" },
- { "mData": "platform.inner" },
- { "mData": "platform.details.0" },
- { "mData": "platform.details.1" }
- ]
- } );
- } );
- // Using mData as a function to provide different information for
- // sorting, filtering and display. In this case, currency (price)
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [ 0 ],
- "mData": function ( source, type, val ) {
- if (type === ‘set‘) {
- source.price = val;
- // Store the computed dislay and filter values for efficiency
- source.price_display = val=="" ? "" : "[ DISCUZ_CODE_7 ]quot;+numberFormat(val);
- source.price_filter = val=="" ? "" : "[ DISCUZ_CODE_7 ]quot;+numberFormat(val)+" "+val;
- return;
- }
- else if (type === ‘display‘) {
- return source.price_display;
- }
- else if (type === ‘filter‘) {
- return source.price_filter;
- }
- // ‘sort‘, ‘type‘ and undefined all just use the integer
- return source.price;
- }
- } ]
- } );
- } );
- integer - treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).
- string - read an object property from the data source. Note that you can use Javascript dotted notation to read deep properties / arrays from the data source and also array brackets to indicate that the data reader should loop over the data source array. When characters are given between the array brackets, these characters are used to join the data source array together. For example: "accounts[, ].name" would result in a comma separated list with the ‘name‘ value from the ‘accounts‘ array of objects.
- function - the function given will be executed whenever DataTables needs to set or get the data for a cell in the column. The function takes three parameters:
- {array|object} The data source for the row (based on mData)
- {string} The type call data requested - this will be ‘filter‘, ‘display‘, ‘type‘ or ‘sort‘.
- {array|object} The full data source for the row (not based on mData)
Default: | null Use mData |
Type: | string |
- // Create a comma separated list from an array of objects
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "sAjaxSource": "sources/deep.txt",
- "aoColumns": [
- { "mData": "engine" },
- { "mData": "browser" },
- {
- "mData": "platform",
- "mRender": "[, ].name"
- }
- ]
- } );
- } );
- // Use as a function to create a link from the data source
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [ 0 ],
- "mData": "download_link",
- "mRender": function ( data, type, full ) {
- return ‘<a href="‘+data+‘">Download</a>‘;
- }
- } ]
- } );
- } );
Default: | td |
Type: | string |
- // Make the first column use TH cells
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [ 0 ],
- "sCellType": "th"
- } ]
- } );
- } );
Default: | Empty string |
Type: | string |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "sClass": "my_class", "aTargets": [ 0 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "sClass": "my_class" },
- null,
- null,
- null,
- null
- ]
- } );
- } );
Default: | Empty string |
Type: | string |
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- null,
- null,
- null,
- {
- "sContentPadding": "mmm"
- }
- ]
- } );
- } );
Default: | null |
Type: | string |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- {
- "mData": null,
- "sDefaultContent": "Edit",
- "aTargets": [ -1 ]
- }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- null,
- null,
- null,
- {
- "mData": null,
- "sDefaultContent": "Edit"
- }
- ]
- } );
- } );
Default: | Empty string |
Type: | string |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "sName": "engine", "aTargets": [ 0 ] },
- { "sName": "browser", "aTargets": [ 1 ] },
- { "sName": "platform", "aTargets": [ 2 ] },
- { "sName": "version", "aTargets": [ 3 ] },
- { "sName": "grade", "aTargets": [ 4 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "sName": "engine" },
- { "sName": "browser" },
- { "sName": "platform" },
- { "sName": "version" },
- { "sName": "grade" }
- ]
- } );
- } );
Default: | std |
Type: | string |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "sSortDataType": "dom-text", "aTargets": [ 2, 3 ] },
- { "sType": "numeric", "aTargets": [ 3 ] },
- { "sSortDataType": "dom-select", "aTargets": [ 4 ] },
- { "sSortDataType": "dom-checkbox", "aTargets": [ 5 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- null,
- null,
- { "sSortDataType": "dom-text" },
- { "sSortDataType": "dom-text", "sType": "numeric" },
- { "sSortDataType": "dom-select" },
- { "sSortDataType": "dom-checkbox" }
- ]
- } );
- } );
Default: | null Derived from the ‘TH‘ value for this column in the original HTML table. |
Type: | string |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "sTitle": "My column title", "aTargets": [ 0 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "sTitle": "My column title" },
- null,
- null,
- null,
- null
- ]
- } );
- } );
Default: | null Auto-detected from raw data |
Type: | string |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "sType": "html", "aTargets": [ 0 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "sType": "html" },
- null,
- null,
- null,
- null
- ]
- } );
- } );
Default: | null Automatic |
Type: | string |
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "sWidth": "20%", "aTargets": [ 0 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "sWidth": "20%" },
- null,
- null,
- null,
- null
- ]
- } );
- } );
fnCookieCallback:尚未使用過 $(document).ready( function () { $(‘#example‘).dataTable( { "fnCookieCallback": function (sName, oData, sExpires, sPath) { // Customise oData or sName or whatever else here return sName + "="+JSON.stringify(oData)+"; expires=" + sExpires +"; path=" + sPath; } } ); } ); fnCreatedRow:顧名思義,建立行得時候的回調函數 $(document).ready( function() { $(‘#example‘).dataTable( { "fnCreatedRow": function( nRow, aData, iDataIndex ) { // 爲a的話字體加粗 if ( aData[4] == "A" ) { $(‘td:eq(4)‘, nRow).html( ‘<b>A</b>‘ ); } } } ); } ); fnDrawCallback:draw畫 ,這個應該是重繪的回調函數 $(document).ready( function() { $(‘#example‘).dataTable( { "fnDrawCallback": function( oSettings ) { alert( ‘DataTables 重繪了‘ ); } } ); } ); fnFooterCallback:底部的回調函數,這個能夠用來作總計之類的功能 $(document).ready( function() { $(‘#example‘).dataTable( { "fnFooterCallback": function( nFoot, aData, iStart, iEnd, aiDisplay ) { nFoot.getElementsByTagName(‘th‘)[0].innerHTML = "Starting index is "+iStart; } } ); } ) fnFormatNumber:顧名思義,格式化數字的顯示方式 $(document).ready( function() { $(‘#example‘).dataTable( { "fnFormatNumber": function ( iIn ) { if ( iIn < 1000 ) { return iIn; } else { var s=(iIn+""), a=s.split(""), out="", iLen=s.length; for ( var i=0 ; i<iLen ; i++ ) { if ( i%3 === 0 && i !== 0 ) { out = "‘"+out; } out = a[iLen-i-1]+out; } } return out; }; } ); } ); fnHeaderCallback:表頭的回調函數 $(document).ready( function() { $(‘#example‘).dataTable( { "fnHeaderCallback": function( nHead, aData, iStart, iEnd, aiDisplay ) { nHead.getElementsByTagName(‘th‘)[0].innerHTML = "Displaying "+(iEnd-iStart)+" records"; } } ); } ) fnInfoCallback:datatables信息的回調函數 $(‘#example‘).dataTable( { "fnInfoCallback": function( oSettings, iStart, iEnd, iMax, iTotal, sPre ) { return iStart +" to "+ iEnd; } } ); fnInitComplete:datatables初始化完畢後會調用這個方法 $(document).ready( function() { $(‘#example‘).dataTable( { "fnInitComplete": function(oSettings, json) { alert( ‘DataTables has finished its initialisation.‘ ); } } ); } ) fnPreDrawCallback:每一次繪datatables時候調用的方法 $(document).ready( function() { $(‘#example‘).dataTable( { "fnPreDrawCallback": function( oSettings ) { if ( $(‘#test‘).val() == 1 ) { return false; } } } ); } ); fnRowCallback:行的回調函數 $(document).ready( function() { $(‘#example‘).dataTable( { "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) { // Bold the grade for all ‘A‘ grade browsers if ( aData[4] == "A" ) { $(‘td:eq(4)‘, nRow).html( ‘<b>A</b>‘ ); } } } ); } ); fnServerData:這個是結合服務器模式的回調函數,用來處理服務器返回過來的數據 // POST data to server $(document).ready( function() { $(‘#example‘).dataTable( { "bProcessing": true, "bServerSide": true, "sAjaxSource": "xhr.php", "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) { oSettings.jqXHR = $.ajax( { "dataType": ‘json‘, "type": "POST", "url": sSource, "data": aoData, "success": fnCallback } ); } } ); } ); fnServerParams:向服務器傳額外的參數 $(document).ready( function() { $(‘#example‘).dataTable( { "bProcessing": true, "bServerSide": true, "sAjaxSource": "scripts/server_processing.php", "fnServerParams": function ( aoData ) { aoData.push( { "name": "more_data", "value": "my_value" } ); } } ); } ); fnStateLoad:讀取狀態的回調函數 $(document).ready( function() { $(‘#example‘).dataTable( { "bStateSave": true, "fnStateLoad": function (oSettings) { var o; // Send an Ajax request to the server to get the data. Note that // this is a synchronous request. $.ajax( { "url": "/state_load", "async": false, "dataType": "json", "success": function (json) { o = json; } } ); return o; } } ); } ); fnStateLoadParams:和上面的不知道什麼區別,沒用過 // Remove a saved filter, so filtering is never loaded $(document).ready( function() { $(‘#example‘).dataTable( { "bStateSave": true, "fnStateLoadParams": function (oSettings, oData) { oData.oSearch.sSearch = ""; } } ); } ); // Disallow state loading by returning false $(document).ready( function() { $(‘#example‘).dataTable( { "bStateSave": true, "fnStateLoadParams": function (oSettings, oData) { return false; } } ); } ); fnStateLoaded:又是這個,加了ed 不知道意思沒用過 // Show an alert with the filtering value that was saved $(document).ready( function() { $(‘#example‘).dataTable( { "bStateSave": true, "fnStateLoaded": function (oSettings, oData) { alert( ‘Saved filter was: ‘+oData.oSearch.sSearch ); } } ); } ); fnStateSave:狀態儲存 $(document).ready( function() { $(‘#example‘).dataTable( { "bStateSave": true, "fnStateSave": function (oSettings, oData) { // Send an Ajax request to the server with the state object $.ajax( { "url": "/state_save", "data": oData, "dataType": "json", "method": "POST" "success": function () {} } ); } } ); } ); fnStateSaveParams :狀態儲存參數,沒用過,不明白 // Remove a saved filter, so filtering is never saved $(document).ready( function() { $(‘#example‘).dataTable( { "bStateSave": true, "fnStateSaveParams": function (oSettings, oData) { oData.oSearch.sSearch = ""; } } ); } );