jQuery - (JQuery datatables api 使用解讀)

學習可參考:http://www.guoxk.com/node/jquery-datatables

??????????????????????http://yuemeiqing2008-163-com.iteye.com/blog/2006942

?

?? 分別導入css和js文件

<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/datatables/css/dataTables.bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Content/datatables/js/jquery.dataTables.js"></script>
<script src="~/Content/datatables/js/dataTables.bootstrap.js"></script>

?加載

Html代碼??
  1. <script?type="text/javascript">??
  2. ????????$(document).ready(function()?{??
  3. ????????????$('#example').dataTable();??
  4. ????????});??
  5. ????</script>??

?表單

Html代碼??
  1. <div?class="col_2_3_right">??
  2. ????????????????<div?class="index_viewport">??
  3. ????????????????????<table?id="example"?cellpadding="0"?cellspacing="0"?border="0"?width="100%">??
  4. ????????????????????????<thead>??
  5. ????????????????????????????<tr>??
  6. ????????????????????????????????<th?width="20%">First?name</th>??
  7. ????????????????????????????????<th?width="20%">Last?name</th>??
  8. ????????????????????????????????<th?width="35%">City</th>??
  9. ????????????????????????????????<th?width="25%">Date</th>??
  10. ????????????????????????????</tr>??
  11. ????????????????????????</thead>??
  12. ????????????????????</table>??
  13. ????????????????</div>??
  14. ????????????</div>??

問題:有時提示找不到datatable方法

原因
window.onload必須等到頁面內包括圖片的所有元素加載完畢后才能執行。?
$(document).ready()是DOM結構繪制完畢后就執行,不必等到加載完畢。

?從后臺ajax得數據重建datatables(表單的id要與json的key一致)

Js代碼??
  1. $.ajax({??
  2. ????????????????????  type:'get',//可選get??
  3. ????????????????????  url:'${projectPath}/search',??
  4. ????????????????????  data:{"channelType":$('#channelType').val(),"channel":$('#channel').val(),"day":$('#day').val(),"startTime":$('#startTime').val(),"endTime":$('#endTime').val(),"database":$('#database').val()},??
  5. ????????????????????  dataType:'text',//服務器返回的數據類型?可選XML?,Json?jsonp?script?htmltext等??
  6. ????????????????????  success:function(msg){??
  7. ????????????????????????var?msgObj=JSON.parse(msg);??
  8. ????????????????????????//重新構建table??
  9. ?????????????????????????$('#example').dataTable().fnClearTable();???//將數據清除??
  10. ???????????????????? ????$('#example').dataTable().fnAddData(packagingdatatabledata(msgObj),true);??//數據必須是json對象或json對象數組??
  11. ???????????????????? ??
  12. ????????????????  },??
  13. ????????????????????  error:function(){??
  14. ????????????????????  alert('error');??
  15. ????????????????????  }??
  16. ????????????})})??

傳入的數據

Js代碼??
  1. //把服務器返回的數據轉成datatable須要的格式??
  2. ????????function?packagingdatatabledata(msgObj){??
  3. ????????????var?editHtml="<a?class='btn'?data-toggle='modal'?href='#modalbackdroptrue'?>編輯</a>";??
  4. ????????????//var?editHtml="<a?class='btn'?href='#modalbackdroptrue'?data-toggle='modal'?>編輯</a>";??
  5. ????????????var?a=[];??
  6. ????????????var?tableName=['day','data','indata','edit'];??
  7. ????????????var?banddata=storjson(msgObj['data']);??
  8. ????????????var?bandindata=storjson(msgObj['indata']);??
  9. ????????????for(var?key?in?banddata){??
  10. ????????????????var?tempObj=new?Object();??
  11. ????????????????tempObj.day=key;??
  12. ????????????????tempObj.data=banddata[key];??
  13. ????????????????tempObj.indata=bandindata[key];??
  14. ????????????????tempObj.edit=editHtml;??
  15. ????????????????a.push(JSON.parse(JSON.stringify(tempObj,tableName)));??
  16. ????????????????}??
  17. ????????????return?a;??
  18. ????????}??

?傳入的是一個對象數組,每個對象代表一行,對象的屬性即是列

?

單擊時得到某行的值

?? 須要引入 jquery.dataTables.nightly.js? 在附件中有??

Js代碼??
  1. $(document).ready(function()?{??
  2. ????/*?Init?DataTables?*/??
  3. ????$('#example').dataTable();??
  4. $('#example?tbody?tr').live('click',?function?()?{??
  5. ????????????????var?sTitle;??
  6. ????????????????var?nTds?=?$('td',?this);??
  7. ????????????????var?sday?=?$(nTds[0]).text();??//得到第1列的值??
  8. ????????????????var?sout?=?$(nTds[1]).text();??
  9. ????????????????var?sin?=?$(nTds[2]).text();??
  10. ??????????????????
  11. ????????????}?);??
  12. ??
  13. }?);??

???

2:詳細配置

Js代碼??
  1. var?docrTable?=?$('#docrevisontable').dataTable({??
  2. ????????????"bProcessing"?:?true,?//DataTables載入數據時,是否顯示‘進度’提示??
  3. ????????????"bServerSide"?:?true,?//是否啟動服務器端數據導入??
  4. ????????????"bStateSave"?:?true,?//是否打開客戶端狀態記錄功能,此功能在ajax刷新紀錄的時候不會將個性化設定回復為初始化狀態??
  5. ????????????"bJQueryUI"?:?true,?//是否使用?jQury的UI?theme??
  6. ????????????"sScrollY"?:?450,?//DataTables的高??
  7. ????????????"sScrollX"?:?820,?//DataTables的寬??
  8. ????????????"aLengthMenu"?:?[20,?40,?60],?//更改顯示記錄數選項??
  9. ????????????"iDisplayLength"?:?40,?//默認顯示的記錄數??
  10. ????????????"bAutoWidth"?:?false,?//是否自適應寬度??
  11. ????????????//"bScrollInfinite"?:?false,?//是否啟動初始化滾動條??
  12. ????????????"bScrollCollapse"?:?true,?//是否開啟DataTables的高度自適應,當數據條數不夠分頁數據條數的時候,插件高度是否隨數據條數而改變??
  13. ????????????"bPaginate"?:?true,?//是否顯示(應用)分頁器??
  14. ????????????"bInfo"?:?true,?//是否顯示頁腳信息,DataTables插件左下角顯示記錄數??
  15. ????????????"sPaginationType"?:?"full_numbers",?//詳細分頁組,可以支持直接跳轉到某頁??
  16. ????????????"bSort"?:?true,?//是否啟動各個字段的排序功能??
  17. ????????????"aaSorting"?:?[[1,?"asc"]],?//默認的排序方式,第2列,升序排列??
  18. ????????????"bFilter"?:?true,?//是否啟動過濾、搜索功能??
  19. ????????????????????"aoColumns"?:?[{??
  20. ????????????????????????"mDataProp"?:?"USERID",??
  21. ????????????????????????"sDefaultContent"?:?"",?//此列默認值為"",以防數據中沒有此值,DataTables加載數據的時候報錯??
  22. ????????????????????????"bVisible"?:?false?//此列不顯示??
  23. ????????????????????},?{??
  24. ????????????????????????"mDataProp"?:?"USERNAME",??
  25. ????????????????????????"sTitle"?:?"用戶名",??
  26. ????????????????????????"sDefaultContent"?:?"",??
  27. ????????????????????????"sClass"?:?"center"??
  28. ????????????????????},?{??
  29. ????????????????????????"mDataProp"?:?"EMAIL",??
  30. ????????????????????????"sTitle"?:?"電子郵箱",??
  31. ????????????????????????"sDefaultContent"?:?"",??
  32. ????????????????????????"sClass"?:?"center"??
  33. ????????????????????},?{??
  34. ????????????????????????"mDataProp"?:?"MOBILE",??
  35. ????????????????????????"sTitle"?:?"手機",??
  36. ????????????????????????"sDefaultContent"?:?"",??
  37. ????????????????????????"sClass"?:?"center"??
  38. ????????????????????},?{??
  39. ????????????????????????"mDataProp"?:?"PHONE",??
  40. ????????????????????????"sTitle"?:?"座機",??
  41. ????????????????????????"sDefaultContent"?:?"",??
  42. ????????????????????????"sClass"?:?"center"??
  43. ????????????????????},?{??
  44. ????????????????????????"mDataProp"?:?"NAME",??
  45. ????????????????????????"sTitle"?:?"姓名",??
  46. ????????????????????????"sDefaultContent"?:?"",??
  47. ????????????????????????"sClass"?:?"center"??
  48. ????????????????????},?{??
  49. ????????????????????????"mDataProp"?:?"ISADMIN",??
  50. ????????????????????????"sTitle"?:?"用戶權限",??
  51. ????????????????????????"sDefaultContent"?:?"",??
  52. ????????????????????????"sClass"?:?"center"??
  53. ????????????????????}],??
  54. ????????????????????"oLanguage":?{?//國際化配置??
  55. ????????????????"sProcessing"?:?"正在獲取數據,請稍后...",????
  56. ????????????????"sLengthMenu"?:?"顯示?_MENU_?條",????
  57. ????????????????"sZeroRecords"?:?"沒有您要搜索的內容",????
  58. ????????????????"sInfo"?:?"從?_START_?到??_END_?條記錄?總記錄數為?_TOTAL_?條",????
  59. ????????????????"sInfoEmpty"?:?"記錄數為0",????
  60. ????????????????"sInfoFiltered"?:?"(全部記錄數?_MAX_?條)",????
  61. ????????????????"sInfoPostFix"?:?"",????
  62. ????????????????"sSearch"?:?"搜索",????
  63. ????????????????"sUrl"?:?"",????
  64. ????????????????"oPaginate":?{????
  65. ????????????????????"sFirst"?:?"第一頁",????
  66. ????????????????????"sPrevious"?:?"上一頁",????
  67. ????????????????????"sNext"?:?"下一頁",????
  68. ????????????????????"sLast"?:?"最后一頁"????
  69. ????????????????}??
  70. ????????????},??
  71. ??????????????????????
  72. ????????????????????"fnRowCallback"?:?function(nRow,?aData,?iDisplayIndex)?{??
  73. ????????????????????????/*?用來改寫用戶權限的?*/??
  74. ????????????????????????if?(aData.ISADMIN?==?'1')??
  75. ????????????????????????????$('td:eq(5)',?nRow).html('管理員');??
  76. ????????????????????????if?(aData.ISADMIN?==?'2')??
  77. ????????????????????????????$('td:eq(5)',?nRow).html('資料下載');??
  78. ????????????????????????if?(aData.ISADMIN?==?'3')??
  79. ????????????????????????????$('td:eq(5)',?nRow).html('一般用戶');??
  80. ??????????????????????????
  81. ????????????????????????return?nRow;??
  82. ????????????????????},??
  83. ??????????????????????
  84. ????????????????????"sAjaxSource"?:?"../use/userList.do?now="?+?new?Date().getTime(),??
  85. ????????????????????????//服務器端,數據回調處理??
  86. ????????????????????????????"fnServerData"?:?function(sSource,?aDataSet,?fnCallback)?{??
  87. ????????????????????????????????$.ajax({??
  88. ????????????????????????????????????"dataType"?:?'json',??
  89. ????????????????????????????????????"type"?:?"POST",??
  90. ????????????????????????????????????"url"?:?sSource,??
  91. ????????????????????????????????????"data"?:?aDataSet,??
  92. ????????????????????????????????????"success"?:?fnCallback??
  93. ????????????????????????????????});??
  94. ????????????????????????????}??
  95. ????????????????});??
  96. ??????????????????
  97. ????????????????$("#docrevisontable?tbody").click(function(event)?{?//當點擊表格內某一條記錄的時候,會將此記錄的cId和cName寫入到隱藏域中??
  98. ????????????????????$(docrTable.fnSettings().aoData).each(function()?{??
  99. ????????????????????????$(this.nTr).removeClass('row_selected');??
  100. ????????????????????});??
  101. ????????????????????$(event.target.parentNode).addClass('row_selected');??
  102. ????????????????????var?aData?=?docrTable.fnGetData(event.target.parentNode);??
  103. ??????????????????????
  104. ????????????????????$("#userId").val(aData.USERID);??
  105. ????????????????????$("#userN").val(aData.USERNAME);??
  106. ????????????????});??
  107. ??????????????????
  108. ????????????????$('#docrevisontable_filter').html('<span>用戶管理列表');??
  109. ????????????????$('#docrevisontable_filter').append('???<input?type="button"?class="addBtn"?id="addBut"?value="創建"/>');??
  110. ????????????????$('#docrevisontable_filter').append('???<input?type="button"?class="addBtn"?id="addBut"?value="修改"/>');??
  111. ????????????????$('#docrevisontable_filter').append('???<input?type="button"?class="addBtn"?id="addBut"?value="刪除"/>');??
  112. ????????????????$('#docrevisontable_filter').append('</span>');??
  113. ????????}??

?設置隱藏列

Js代碼??
  1. var?oTable?=?$('#example').dataTable();??
  2. oTable.fnSetColumnVis(?0,?false);//隱藏列??

?得到當前頁面中的數據

var alldata=$('#example').dataTable().fnGetData();//得到頁面中所有對象

?

// Row data?
$(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' , 1 , 1 ,false); //coll?
$('#example').dataTable().fnUpdate( ['a','b'] , 1 ); //row

?得到當前瀏覽的datatables頁碼

Js代碼??
  1. var?tableSetings=$('#example').dataTable().fnSettings()??
  2. var?paging_length=tableSetings._iDisplayLength;//當前每頁顯示多少??
  3. var?page_start=tableSetings._iDisplayStart;//當前頁開始??
  4. var?page=Div(page_start,paging_length);??
  5. ??
  6. function?Div(exp1,?exp2)?{??//整除??
  7. ????var?n1?=?Math.round(exp1);?//四舍五入?????
  8. ????var?n2?=?Math.round(exp2);?//四舍五入????
  9. ??
  10. ????var?rslt?=?n1?/?n2;?//除????
  11. ????if?(rslt?>=?0)?{??
  12. ????????rslt?=?Math.floor(rslt);?//返回小于等于原rslt的最大整數。?????
  13. ????}??
  14. ????else?{??
  15. ????????rslt?=?Math.ceil(rslt);?//返回大于等于原rslt的最小整數。?????
  16. ????}??
  17. ????return?rslt;??
  18. }??

?設置datatables跳轉到某頁

Js代碼??
  1. $('#example').dataTable().fnPageChange(page);???

?注意:由于后臺數據可能已經被其它人改變(記錄個數與現在前臺不一致),所以數據fnUpdate時須要判斷前后端數據的一致性,只刷 新前臺有的數據的狀態

?

dom:

http://datatables.net/release-datatables/examples/api/select_single_row.html?選擇一行
http://datatables.net/release-datatables/examples/api/select_row.html選擇多行
http://datatables.net/release-datatables/examples/api/editable.html?即時編輯行
http://datatables.net/release-datatables/examples/api/tabs_and_scrolling.html?多個tab
http://datatables.net/release-datatables/examples/api/add_row.html添加一行(靜態)
http://datatables.net/release-datatables/examples/api/multi_filter.html多列搜索
http://datatables.net/release-datatables/examples/api/multi_filter_select.html多列搜索(擴展)
http://datatables.net/release-datatables/examples/api/highlight.html行列 高亮
http://datatables.net/release-datatables/examples/advanced_init/highlight.html?鼠標移上去亮
http://datatables.net/release-datatables/examples/api/row_details.html行詳細信息
http://datatables.net/release-datatables/examples/api/counter_column.html添加行數
http://datatables.net/release-datatables/examples/api/show_hide.html隱藏列
http://datatables.net/release-datatables/examples/api/api_in_init.html點中即為搜索條件
http://datatables.net/release-datatables/examples/advanced_init/events_live.html?給每一個行添加事件顯示行的信息
http://datatables.net/release-datatables/examples/advanced_init/events_pre_init.html鼠標事件 移到某一行顯示信息
http://datatables.net/release-datatables/examples/advanced_init/events_post_init.html?同上
http://datatables.net/release-datatables/examples/advanced_init/dom_multiple_elements.html?用sdom屬性控制插件位置
http://datatables.net/release-datatables/examples/advanced_init/dom_toolbar.htmlsdom應用
http://datatables.net/release-datatables/examples/advanced_init/length_menu.html更改按多少數據顯示
http://datatables.net/release-datatables/examples/advanced_init/complex_header.html表頭組
http://datatables.net/release-datatables/examples/advanced_init/row_grouping.html按組顯示行
http://datatables.net/release-datatables/examples/advanced_init/row_callback.html列回調函數
http://datatables.net/release-datatables/examples/advanced_init/footer_callback.html總計(footer回調)
http://datatables.net/release-datatables/examples/advanced_init/sorting_control.html自定義排序

http://datatables.net/release-datatables/examples/advanced_init/language_file.html國際化

?

一:api

bAutoWidth?:啟用或禁用自動列寬度的計算。

默認值true
類型boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bAutoWidth": false??//關閉后,表格將不會自動計算表格大小,在瀏覽器大化小化的時候會擠在一坨
  4. ? ? } );
  5. } );

bDeferRender?:根據官網的介紹翻譯過來就是,延期渲染,可以有個速度的提升,當datatable 使用Ajax或者JS源表的數據。這個選項設置為true,將導致datatable推遲創建表元素每個元素,直到他們都創建完成——本參數的目的是節省大量的時間。

默認值:false
類型:boolean
  1. $(document).ready( function() {
  2. ? ? var oTable = $('#example').dataTable( {
  3. ? ?? ???"sAjaxSource": "sources/arrays.txt",
  4. ? ?? ???"bDeferRender": true? ?//這個參數我個人沒有使用過,到底是不是這個效果,大家自己去嘗試一下
  5. ? ? } );
  6. } );

bFilter?:這個很容易明白,啟用或禁用過濾數據。在datatable過濾是“智能”,它允許用戶 最終輸入多個關鍵字(空格分隔),和每一行數據匹配,即使不是在被指定的順序(這允許匹配多個列)。注意,如果您希望使用過濾,在datatable中必須設置為true,如果要刪除默認過濾輸入框和留住過濾功能,請使用{ @link DataTable.defaults.sDom }。這個最后一句不明白,不知道大家怎么理解。

默認值:true
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bFilter": false
  4. ? ? } );
  5. } );

bJQueryUI?:這個不用多說了,一看就懂,啟用jQuery UI樣式,(需要在頁面添加jQuery的樣式文件)。

默認值:false
類型:boolean
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ? "bJQueryUI": true
  4. ? ? } );
  5. } );

bLengthChange?:開啟一頁顯示多少條數據的下拉菜單,允許用戶從下拉框(10、25、50和100),注意需要分頁(bPaginate:true)。

默認值:true
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bLengthChange": false,

bPaginate?:開啟分頁功能,如果不開啟,將會全部顯示

默認值:true
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bPaginate": false? ?
  4. ? ? } );
  5. } );

bProcessing?:開啟讀取服務器數據時顯示正在加載中……特別是大數據量的時候,開啟此功能比較好

默認值:false
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bProcessing": true
  4. ? ? } );
  5. } );

bScrollInfinite?:是否開啟不限制長度的滾動條(和sScrollY屬性結合使用),不限制長度的滾動條意味著當用戶拖動滾動條的時候DataTable會不斷加載數據當數據集十分大的時候會有些用處,該選項無法和分頁選項同時使用,分頁選項會被自動禁止,注意,額外推薦的滾動條會優先與該選項

默認值:false
類型:boolean
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bScrollInfinite": true,
  4. ? ?? ???"bScrollCollapse": true,
  5. ? ?? ???"sScrollY": "200px"//長200像素
  6. ? ? } );
  7. } );

bServerSide?:開啟服務器模式,使用服務器端處理配置datatable。注意:sAjaxSource參數也必須被給予為了給datatable源代碼來獲取所需的數據對于每個畫。 這個翻譯有點別扭。開啟此模式后,你對datatables的每個操作 每頁顯示多少條記錄、下一頁、上一頁、排序(表頭)、搜索,這些都會傳給服務器相應的值。

默認值:false
類型:boolean
  1. $(document).ready( function () {
  2. ? ?? ?$('#example').dataTable( {
  3. ? ?? ?? ? "bServerSide": true,
  4. ? ?? ?? ? "sAjaxSource": "xhr.php"
  5. ? ?? ? } );
  6. } );

bInfo?:啟用或禁用表信息顯示。這顯示了關于數據的信息,目前在網頁上,包括信息過濾數據,如果行為被執行。這個參數在bServerSide配置后需要用到。

默認值:true
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ? "bInfo": false//如果這個參數不穿到后臺去,服務器分頁會報錯,據說這個參數包含了表的所有信息
  4. ? ? } );
  5. } );

bSort?:開啟排序功能,每一列都有排序功能,如果關閉了,排序功能將失效,也可以自定義排序功能。單個列的排序可以禁用“bSortable”選項。

默認值:true
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bSort": false
  4. ? ? } );
  5. } );

bSortClasses?:是否在當前被排序的列上額外添加sorting_1,sorting_2,sorting_3三個class,當該列被排序的時候,可以切換其背景顏色,該選項作為一個來回切換的屬性會增加執行時間(當class被移除和添加的時候),當對大數據集進行排序的時候你或許希望關閉該選項

默認值:true
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bSortClasses": false
  4. ? ???} );
  5. } );

bStateSave?:是否開啟狀態保存,當選項開啟的時候會使用一個cookie保存表格展示的信息的狀態,例如分頁信息,展示長度,過濾和排序等。這樣當終端用戶重新加載這個頁面的時候可以使用以前的設置

默認值:false
類型:boolean
  1. $(document).ready( function () {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bStateSave": true
  4. ? ? } );
  5. } );

sScrollX?:是否開啟水平滾動,當一個表格過于寬以至于無法放入一個布局的時候,或者表格有太多列的時候,你可以開啟該選項從而在一個可橫向滾動的視圖里面展示表格,該屬性可以是css設置,或者一個數字(作為像素量度來使用)

默認值:blank string - i.e. disabled
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"sScrollX": "100%",
  4. ? ?? ???"bScrollCollapse": true
  5. ? ???} );
  6. } );

sScrollY:是否開啟垂直滾動,垂直滾動會驅使DataTable設置為給定的長度,任何溢出到當前視圖之外的數據可以通過垂直滾動進行察看當在小范圍區域中顯示大量數據的時候,可以在分頁和垂直滾動中選擇一種方式,該屬性可以是css設置,或者一個數字(作為像素量度來使用)

默認值:blank string - i.e. disabled
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"sScrollY": "200px",
  4. ? ?? ???"bPaginate": false
  5. ? ???} );
  6. } );

bDestroy?:使用傳遞的新的初始化對象中的屬性構造一個新的表格,并替換一個匹配指定的選擇器的表格,如果沒有匹配到表格,新的表格會被作為一個普通表格被構建

默認值:false
類型:boolean
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"sScrollY": "200px",
  4. ? ?? ???"bPaginate": false
  5. ? ? } );
  6. ? ?
  7. ??// Some time later....
  8. ? ? $('#example').dataTable( {
  9. ? ?? ???"bFilter": false,
  10. ? ?? ???"bDestroy": true
  11. ? ? } );
  12. } );

bRetrieve?:使用指定的選擇器檢索表格,注意,如果表格已經被初始化,該參數會直接返回已經被創建的對象,并不會顧及你傳遞進來的初始化參數對象的變化,將該參數設置為true說明你確認已經明白這一點,如果你需要的話,bDestroy可以用來重新初始化表格

默認值:false
類型:boolean
  1. $(document).ready( function() {
  2. ? ? initTable();
  3. ? ? tableActions();
  4. } );
  5. function initTable ()
  6. {
  7. ? ? return $('#example').dataTable( {
  8. ? ? "sScrollY": "200px",
  9. ? ? "bPaginate": false,
  10. ? ? "bRetrieve": true
  11. ? ? } );
  12. }
  13. function tableActions ()
  14. {
  15. ? ? var oTable = initTable();
  16. ? ? // perform API operations with oTable
  17. }

bScrollAutoCss?:指明DataTable中滾動的標題元素是否被允許設置內邊距和外邊距等

默認值:true
類型:boolean
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bScrollAutoCss": false,
  4. ? ?? ???"sScrollY": "200px"
  5. ? ? } );
  6. } );

bScrollCollapse?:當垂直滾動被允許的時候,DataTable會強制表格視圖在任何時候都是給定的高度(對布局有利)不過,當把數據集過濾到十分小的時候看起來會很古怪,而且頁腳會留在最下面當結果集的高度比給定的高度小時該參數會使表格高度自適應

默認值:false
類型:
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"sScrollY": "200",
  4. ? ?? ???"bScrollCollapse": true
  5. ? ? } );
  6. } );

bSortCellsTop?:是否允許DataTable使用頂部(默認為true)的單元格,或者底部(默認為false)的單元格,當使用復合表頭的時候會有些用處

默認值:false
類型:boolean
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bSortCellsTop": true
  4. ? ? } );
  5. } );

iCookieDuration?:指定用于存儲客戶端信息到cookie中的時間長度,超過這個時間后,自動過期

默認值:7200?(2 hours)
類型:int
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ? "iCookieDuration": 60*60*24; // 一天
  4. ? ? } );
  5. } )

iDeferLoading?:當選項被開啟的時候,DataTable在非加載第一次的時候不會向服務器請求數據,而是會使用頁面上的已有數據(不會應用排序等),因此在加載的時候保留一個XmlHttpRequest,iDeferLoading被用來指明需要延遲加載,而且也用來通知DataTable一個滿的表格有多少條數據,信息元素和分頁會被正確保留

默認值:null
類型:int
  1. // 57 records available in the table, no filtering applied
  2. $(document).ready( function() {
  3. ? ? $('#example').dataTable( {
  4. ? ?? ???"bServerSide": true,
  5. ? ?? ???"sAjaxSource": "scripts/server_processing.php",
  6. ? ?? ???"iDeferLoading": 57
  7. ? ? } );
  8. } );
  9. // 57 records after filtering, 100 without filtering (an initial filter applied)
  10. $(document).ready( function() {
  11. ? ???$('#example').dataTable( {
  12. ? ?? ???"bServerSide": true,
  13. ? ?? ???"sAjaxSource": "scripts/server_processing.php",
  14. ? ?? ???"iDeferLoading": [ 57, 100 ],
  15. ? ?? ?? ?? ?"oSearch": {
  16. ? ?? ?? ?? ?"sSearch": "my_filter"
  17. ? ?? ?? ? }
  18. ? ?? ?} );
  19. } );

iDisplayLength?:單頁顯示的數據的條數,如果bLengthChange屬性被開啟,終端用戶可以通過一個彈出菜單重寫該數值

默認值:10
類型:int
  1. $(document).ready( function() {
  2. ? ?? ? $('#example').dataTable( {
  3. ? ?? ?? ?? ?"iDisplayLength": 50
  4. ? ?? ???} );
  5. } )

iDisplayStart?:當開啟分頁的時候,定義展示的記錄的起始序號,不是頁數,因此如果你每個分頁有10條記錄而且想從第三頁開始,需要把該參數指定為20

默認值0
類型:int
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"iDisplayStart": 20
  4. ? ? } );
  5. } )

iScrollLoadGap?:滾動余界是指DataTable在當前頁面還有多少條數據可供滾動時自動加載新的數據,你可能希望指定一個足夠大的余界,以便滾動加載數據的操作對用戶來說是平滑的,同時也不會大到加載比需要的多的多的數據

默認值:100
類型:int
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"bScrollInfinite": true,
  4. ? ?? ???"bScrollCollapse": true,
  5. ? ?? ???"sScrollY": "200px",
  6. ? ?? ???"iScrollLoadGap": 50
  7. ? ? } );
  8. } );

iTabIndex?:默認情況下DataTable允許通過為需要鍵盤導航的元素添加tabindex屬性來進行導航(排序、分頁、過濾),允許你通過tab鍵切換控制組件,使用回車鍵去激活他們,默認為0表示按照文檔流來切換,如果需要的話,你可以使用該參數重寫切換順序,使用-1來禁止內建的鍵盤導航

默認值:0
類型:int
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"iTabIndex": 1
  4. ? ? } );
  5. } );

oSearch?:該參數允許你在初始化的時候使用已經定義的全局過濾狀態,sSearch對象必須被定義,但是所有的其它選項都是可選的,當bRegex為true的時候,搜索字符串會被當作正則表達式,當為false(默認)的時候,會被直接當作一個字符串,當bSmart為true的時候,DataTable會使用使用靈活過濾策略(匹配任何可能的數據),為false的時候不會這樣做

  1. $(document).ready( function() {
  2. ? ?? ?$('#example').dataTable( {
  3. ? ?? ?? ? "oSearch": {"sSearch": "Initial search"}
  4. ? ?? ? } );
  5. } )

sAjaxDataProp?:當使用Ajax數據源或者服務器端處理的時候,DataTable會默認搜索aaData屬性作為數據源,該選項允許變更數據源的名稱,你可以使用JavaScript的點號對象表示法去訪問多級網狀數據源

默認值:aaData
類型:string
  1. // Get data from { "data": [...] }
  2. $(document).ready( function() {
  3. ? ? var oTable = $('#example').dataTable( {
  4. ? ?? ???"sAjaxSource": "sources/data.txt",
  5. ? ?? ? "sAjaxDataProp": "data"
  6. ? ? } );
  7. } );
  8. // Get data from { "data": { "inner": [...] } }
  9. $(document).ready( function() {
  10. ? ? var oTable = $('#example').dataTable( {
  11. ? ?? ? "sAjaxSource": "sources/data.txt",
  12. ? ?? ?"sAjaxDataProp": "data.inner"
  13. ? ?} );
  14. } );

sAjaxSource?:該參數用來向DataTable指定加載的外部數據源(如果想使用現有的數據,請使用aData),可以簡單的提供一個可以用來獲得數據url或者JSON對象,該對象必須包含aaData,作為表格的數據源

默認值:null
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ???"sAjaxSource": "list.action"
  4. ? ???} );
  5. } )

sCookiePrefix?:該參數可以用來重寫DataTable默認指定的用來存儲狀態信息的cookie的前綴

默認值:SpryMedia_DataTables_
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ?"sCookiePrefix": "my_datatable_"
  4. ? ? } );
  5. } );

sDom?:這是用于定義DataTable布局的一個強大的屬性,包括分頁,顯示多少條數據和搜索,格式如下:

  1. The following options are allowed:
  2. ? ? 'l' - 左上角按個下拉框,10個,20個,50個,所有的哪個
  3. ? ? 'f' - 快速過濾框
  4. ? ? 't' - 表格本身
  5. ? ? 'i' - 分頁信息
  6. ? ? 'p' -分頁按鈕
  7. ? ? 'r' - 現在正在加載中……
  8. The following constants are allowed:
  9. ? ? 'H' - jQueryUI theme "header" classes ('fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix')
  10. ? ? 'F' - jQueryUI theme "footer" classes ('fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix')
  11. The following syntax is expected:
  12. ? ? '<' and '>' - div 元素
  13. ? ? '<"class" and '>' - 給div加clasa
  14. ? ? '<"#id" and '>' - 給div加上id
  15. Examples:
  16. ? ? '<"wrapper"flipt>'
  17. ? ? '<lf<t>ip>'
默認值:lfrtip?(when bJQueryUI is false)?or?<"H"lfr>t<"F"ip>?(when bJQueryUI is true)
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ? "sDom": '<"top"i>rt<"bottom"flp><"clear">'
  4. ? ? } );
  5. } );
'<"top"i>rt<"bottom"flp><"clear">'


這段代碼翻譯為html就是這樣子的:

  1. <div class="top">
  2. ? ? i
  3. </div>
  4. rt
  5. <div class="bottom">
  6. ? ? flp
  7. </div>
  8. <div class="clear"></div>

這樣一對比起來,就容易理解多了.Datatables之強大的sDom屬性的應用
sPaginationType?:DataTable內建了兩種交互式分頁策略,兩個按鈕和全頁數,展現給終端用戶不同的控制方式,可以通過API增加策略

默認值:two_button
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ? "sPaginationType": "full_numbers"
  4. ? ? } );
  5. } )

sScrollXInner?:當橫向滾動可用的時候,該屬性可以用來強制DataTable的寬度比需要的更長,比如你需要表格彼此相隔適宜,該變量可以用來使表格變大,而且強制滾動,該該屬性可以是css設置,或者一個數字(作為像素量度來使用)

默認值:blank string - i.e. disabled
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ? "sScrollX": "100%",
  4. ? ?? ? "sScrollXInner": "110%"
  5. ? ?} );
  6. } );

sServerMethod?:設置使用Ajax方式調用的服務器端的處理方法或者Ajax數據源的HTTP請求方式

默認值:GET
類型:string
  1. $(document).ready( function() {
  2. ? ? $('#example').dataTable( {
  3. ? ?? ? "bServerSide": true,
  4. ? ?? ? "sAjaxSource": "list.action",
  5. ? ?? ? "sServerMethod": "POST"? ?//以post的方式提交數據
  6. ? ? } );
  7. } )
二:?列屬性
aoColumnDefs: This array allows you to target a specific column, multiple columns, or all columns, using the aTargets property of each object in the array (please note that aoColumnDefs was introduced in DataTables 1.7). This allows great flexibility when creating tables, as the aoColumnDefs arrays can be of any length, targeting the columns you specifically want. The aTargets property is an array to target one of many columns and each element in it can be:

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
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "aDataSort": [ 0, 1 ], "aTargets": [ 0 ] },
  6. ? ?? ?{ "aDataSort": [ 1, 0 ], "aTargets": [ 1 ] },
  7. ? ?? ?{ "aDataSort": [ 2, 3, 4 ], "aTargets": [ 2 ] }
  8. ? ? ]
  9. ??} );
  10. } );
  11. // Using aoColumns
  12. $(document).ready( function() {
  13. ??$('#example').dataTable( {
  14. ? ? "aoColumns": [
  15. ? ?? ?{ "aDataSort": [ 0, 1 ] },
  16. ? ?? ?{ "aDataSort": [ 1, 0 ] },
  17. ? ?? ?{ "aDataSort": [ 2, 3, 4 ] },
  18. ? ?? ?null,
  19. ? ?? ?null
  20. ? ? ]
  21. ??} );
  22. } );
asSorting:控制列的升序或降序到自定義列

Default:[ 'asc', 'desc' ]
Type:array
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "asSorting": [ "asc" ], "aTargets": [ 1 ] },
  6. ? ?? ?{ "asSorting": [ "desc", "asc", "asc" ], "aTargets": [ 2 ] },
  7. ? ?? ?{ "asSorting": [ "desc" ], "aTargets": [ 3 ] }
  8. ? ? ]
  9. ??} );
  10. } );
  11. // Using aoColumns
  12. $(document).ready( function() {
  13. ??$('#example').dataTable( {
  14. ? ? "aoColumns": [
  15. ? ?? ?null,
  16. ? ?? ?{ "asSorting": [ "asc" ] },
  17. ? ?? ?{ "asSorting": [ "desc", "asc", "asc" ] },
  18. ? ?? ?{ "asSorting": [ "desc" ] },
  19. ? ?? ?null
  20. ? ? ]
  21. ??} );
  22. } );
bSearchable:設置列是否能被快速檢索框搜索到
Default:true
Type:boolean
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "bSearchable": false, "aTargets": [ 0 ] }
  6. ? ? ] } );
  7. } );
  8. // Using aoColumns
  9. $(document).ready( function() {
  10. ??$('#example').dataTable( {
  11. ? ? "aoColumns": [
  12. ? ?? ?{ "bSearchable": false },
  13. ? ?? ?null,
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null
  17. ? ? ] } );
  18. } );
bSortable:啟用或禁用列排序。
Default:true
Type:boolean
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "bSortable": false, "aTargets": [ 0 ] }
  6. ? ? ] } );
  7. } );
  8. // Using aoColumns
  9. $(document).ready( function() {
  10. ??$('#example').dataTable( {
  11. ? ? "aoColumns": [
  12. ? ?? ?{ "bSortable": false },
  13. ? ?? ?null,
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null
  17. ? ? ] } );
  18. } );
bUseRendered?:高版本已經廢棄這個屬性,沒有使用過,直接翻譯吧!當使用fnRender()為一個列,您可能希望使用原始的數據(在呈現之前)進行排序和過濾(默認是用于呈現的數據,用戶可以看到)。這可能是有用的日期等。請注意,該選項已被棄用,將被刪除的下一個版本的datatable。請用mRender / mData而不是fnRender。
Default:true
Type:boolean

無例子代碼;
bVisible:啟用或禁用本列顯示。
Default:true
Type:boolean
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "bVisible": false, "aTargets": [ 0 ] }
  6. ? ? ] } );
  7. } );
  8. // Using aoColumns
  9. $(document).ready( function() {
  10. ??$('#example').dataTable( {
  11. ? ? "aoColumns": [
  12. ? ?? ?{ "bVisible": false },
  13. ? ?? ?null,
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null
  17. ? ? ] } );
  18. } );
fnCreatedCell:開發人員可定義的函數,就會調用一個細胞被創建(Ajax源等)或處理輸入(DOM源)。這可以作為一種恭維,mRender允許您修改DOM元素(例如添加背景顏色)當元素是可用的。

Default:?
Type:function
  1. $(document).ready( function() {
  2. ??$('#example').dataTable( {
  3. ? ? "aoColumnDefs": [ {
  4. ? ?? ?"aTargets": [3],
  5. ? ?? ?"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
  6. ? ?? ???if ( sData == "1.7" ) {
  7. ? ?? ?? ? $(nTd).css('color', 'blue')
  8. ? ?? ???}
  9. ? ?? ?}
  10. ? ? } ]
  11. ??});
  12. } );
fnRender:高版本已經廢棄這個屬性。mRender這個代替

iDataSort:列索引(從0開始!),你想要執行一個在本專欄時被選中進行排序。這可以用于排序在隱藏列例如。這個也沒使用過
Default:-1?Use automatically calculated column index
Type:int
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "iDataSort": 1, "aTargets": [ 0 ] }
  6. ? ? ]
  7. ??} );
  8. } );
  9. // Using aoColumns
  10. $(document).ready( function() {
  11. ??$('#example').dataTable( {
  12. ? ? "aoColumns": [
  13. ? ?? ?{ "iDataSort": 1 },
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null,
  17. ? ?? ?null
  18. ? ? ]
  19. ??} );
  20. } );
mData:這個屬性可以用來讀取JSON數據從任何數據源屬性,包括深層嵌套對象/屬性。可以給mData在許多不同的方面影響其行為:

  • 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'.
    The return value from the function is not required when 'set' is the type of call, but otherwise the return is what will be used for the data requested.
Default:null?Use automatically calculated column index
Type:string
  1. // Read table data from objects
  2. $(document).ready( function() {
  3. ??var oTable = $('#example').dataTable( {
  4. ? ? "sAjaxSource": "sources/deep.txt",
  5. ? ? "aoColumns": [
  6. ? ?? ?{ "mData": "engine" },
  7. ? ?? ?{ "mData": "browser" },
  8. ? ?? ?{ "mData": "platform.inner" },
  9. ? ?? ?{ "mData": "platform.details.0" },
  10. ? ?? ?{ "mData": "platform.details.1" }
  11. ? ? ]
  12. ??} );
  13. } );
  14. // Using mData as a function to provide different information for
  15. // sorting, filtering and display. In this case, currency (price)
  16. $(document).ready( function() {
  17. ??var oTable = $('#example').dataTable( {
  18. ? ? "aoColumnDefs": [ {
  19. ? ?? ?"aTargets": [ 0 ],
  20. ? ?? ?"mData": function ( source, type, val ) {
  21. ? ?? ???if (type === 'set') {
  22. ? ?? ?? ? source.price = val;
  23. ? ?? ?? ? // Store the computed dislay and filter values for efficiency
  24. ? ?? ?? ? source.price_display = val=="" ? "" : "[? ?? ???DISCUZ_CODE_7? ?? ???]quot;+numberFormat(val);
  25. ? ?? ?? ? source.price_filter??= val=="" ? "" : "[? ?? ???DISCUZ_CODE_7? ?? ???]quot;+numberFormat(val)+" "+val;
  26. ? ?? ?? ? return;
  27. ? ?? ???}
  28. ? ?? ???else if (type === 'display') {
  29. ? ?? ?? ? return source.price_display;
  30. ? ?? ???}
  31. ? ?? ???else if (type === 'filter') {
  32. ? ?? ?? ? return source.price_filter;
  33. ? ?? ???}
  34. ? ?? ???// 'sort', 'type' and undefined all just use the integer
  35. ? ?? ???return source.price;
  36. ? ?? ?}
  37. ? ? } ]
  38. ??} );
  39. } );
mRender?:This property is the rendering partner to mData and it is suggested that when you want to manipulate data for display (including filtering, sorting etc) but not altering the underlying data for the table, use this property. mData can actually do everything this property can and more, but this parameter is easier to use since there is no 'set' option. Like mData is can be given in a number of different ways to effect its behaviour, with the addition of supporting array syntax for easy outputting of arrays (including arrays of objects):

  • 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)
    The return value from the function is what will be used for the data requested.
Default:null?Use mData
Type:string
  1. // Create a comma separated list from an array of objects
  2. $(document).ready( function() {
  3. ??var oTable = $('#example').dataTable( {
  4. ? ? "sAjaxSource": "sources/deep.txt",
  5. ? ? "aoColumns": [
  6. ? ?? ?{ "mData": "engine" },
  7. ? ?? ?{ "mData": "browser" },
  8. ? ?? ?{
  9. ? ?? ???"mData": "platform",
  10. ? ?? ???"mRender": "[, ].name"
  11. ? ?? ?}
  12. ? ? ]
  13. ??} );
  14. } );
  15. // Use as a function to create a link from the data source
  16. $(document).ready( function() {
  17. ??var oTable = $('#example').dataTable( {
  18. ? ? "aoColumnDefs": [ {
  19. ? ?? ?"aTargets": [ 0 ],
  20. ? ?? ?"mData": "download_link",
  21. ? ?? ?"mRender": function ( data, type, full ) {
  22. ? ?? ???return '<a href="'+data+'">Download</a>';
  23. ? ?? ?}
  24. ? ? } ]
  25. ??} );
  26. } );
sCellType:Change the cell type created for the column - either TD cells or TH cells. This can be useful as TH cells have semantic meaning in the table body, allowing them to act as a header for a row (you may wish to add scope='row' to the TH elements).
Default:td
Type:string
  1. // Make the first column use TH cells
  2. $(document).ready( function() {
  3. ??var oTable = $('#example').dataTable( {
  4. ? ? "aoColumnDefs": [ {
  5. ? ?? ?"aTargets": [ 0 ],
  6. ? ?? ?"sCellType": "th"
  7. ? ? } ]
  8. ??} );
  9. } );
sClass:給列加上自己定義的屬性
Default:Empty string
Type:string
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "sClass": "my_class", "aTargets": [ 0 ] }
  6. ? ? ]
  7. ??} );
  8. } );
  9. // Using aoColumns
  10. $(document).ready( function() {
  11. ??$('#example').dataTable( {
  12. ? ? "aoColumns": [
  13. ? ?? ?{ "sClass": "my_class" },
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null,
  17. ? ?? ?null
  18. ? ? ]
  19. ??} );
  20. } );
sContentPadding?:When DataTables calculates the column widths to assign to each column, it finds the longest string in each column and then constructs a temporary table and reads the widths from that. The problem with this is that "mmm" is much wider then "iiii", but the latter is a longer string - thus the calculation can go wrong (doing it properly and putting it into an DOM object and measuring that is horribly(!) slow). Thus as a "work around" we provide this option. It will append its value to the text that is found to be the longest string for the column - i.e. padding. Generally you shouldn't need this, and it is not documented on the general DataTables.net documentation

Default:Empty string
Type:string
  1. // Using aoColumns
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumns": [
  5. ? ?? ?null,
  6. ? ?? ?null,
  7. ? ?? ?null,
  8. ? ?? ?{
  9. ? ?? ???"sContentPadding": "mmm"
  10. ? ?? ?}
  11. ? ? ]
  12. ??} );
  13. } );
sDefaultContent?:Allows a default value to be given for a column's data, and will be used whenever a null data source is encountered (this can be because mData is set to null, or because the data source itself is null).
Default:null
Type:string
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{
  6. ? ?? ???"mData": null,
  7. ? ?? ???"sDefaultContent": "Edit",
  8. ? ?? ???"aTargets": [ -1 ]
  9. ? ?? ?}
  10. ? ? ]
  11. ??} );
  12. } );
  13. // Using aoColumns
  14. $(document).ready( function() {
  15. ??$('#example').dataTable( {
  16. ? ? "aoColumns": [
  17. ? ?? ?null,
  18. ? ?? ?null,
  19. ? ?? ?null,
  20. ? ?? ?{
  21. ? ?? ???"mData": null,
  22. ? ?? ???"sDefaultContent": "Edit"
  23. ? ?? ?}
  24. ? ? ]
  25. ??} );
  26. } );
sName:This parameter is only used in DataTables' server-side processing. It can be exceptionally useful to know what columns are being displayed on the client side, and to map these to database fields. When defined, the names also allow DataTables to reorder information from the server if it comes back in an unexpected order (i.e. if you switch your columns around on the client-side, your server-side code does not also need updating).

Default:Empty string
Type:string
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "sName": "engine", "aTargets": [ 0 ] },
  6. ? ?? ?{ "sName": "browser", "aTargets": [ 1 ] },
  7. ? ?? ?{ "sName": "platform", "aTargets": [ 2 ] },
  8. ? ?? ?{ "sName": "version", "aTargets": [ 3 ] },
  9. ? ?? ?{ "sName": "grade", "aTargets": [ 4 ] }
  10. ? ? ]
  11. ??} );
  12. } );
  13. // Using aoColumns
  14. $(document).ready( function() {
  15. ??$('#example').dataTable( {
  16. ? ? "aoColumns": [
  17. ? ?? ?{ "sName": "engine" },
  18. ? ?? ?{ "sName": "browser" },
  19. ? ?? ?{ "sName": "platform" },
  20. ? ?? ?{ "sName": "version" },
  21. ? ?? ?{ "sName": "grade" }
  22. ? ? ]
  23. ??} );
  24. } );
sSortDataType?:Defines a data source type for the sorting which can be used to read real-time information from the table (updating the internally cached version) prior to sorting. This allows sorting to occur on user editable elements such as form inputs.
Default:std
Type:string
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "sSortDataType": "dom-text", "aTargets": [ 2, 3 ] },
  6. ? ?? ?{ "sType": "numeric", "aTargets": [ 3 ] },
  7. ? ?? ?{ "sSortDataType": "dom-select", "aTargets": [ 4 ] },
  8. ? ?? ?{ "sSortDataType": "dom-checkbox", "aTargets": [ 5 ] }
  9. ? ? ]
  10. ??} );
  11. } );
  12. // Using aoColumns
  13. $(document).ready( function() {
  14. ??$('#example').dataTable( {
  15. ? ? "aoColumns": [
  16. ? ?? ?null,
  17. ? ?? ?null,
  18. ? ?? ?{ "sSortDataType": "dom-text" },
  19. ? ?? ?{ "sSortDataType": "dom-text", "sType": "numeric" },
  20. ? ?? ?{ "sSortDataType": "dom-select" },
  21. ? ?? ?{ "sSortDataType": "dom-checkbox" }
  22. ? ? ]
  23. ??} );
  24. } );
sTitle?:The title of this column.
Default:null?Derived from the 'TH' value for this column in the original HTML table.
Type:string
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "sTitle": "My column title", "aTargets": [ 0 ] }
  6. ? ? ]
  7. ??} );
  8. } );
  9. // Using aoColumns
  10. $(document).ready( function() {
  11. ??$('#example').dataTable( {
  12. ? ? "aoColumns": [
  13. ? ?? ?{ "sTitle": "My column title" },
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null,
  17. ? ?? ?null
  18. ? ? ]
  19. ??} );
  20. } );
sType:The type allows you to specify how the data for this column will be sorted. Four types (string, numeric, date and html (which will strip HTML tags before sorting)) are currently available. Note that only date formats understood by Javascript's Date() object will be accepted as type date. For example: "Mar 26, 2008 5:03 PM". May take the values: 'string', 'numeric', 'date' or 'html' (by default). Further types can be adding through plug-ins.
Default:null?Auto-detected from raw data
Type:string
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "sType": "html", "aTargets": [ 0 ] }
  6. ? ? ]
  7. ??} );
  8. } );
  9. // Using aoColumns
  10. $(document).ready( function() {
  11. ??$('#example').dataTable( {
  12. ? ? "aoColumns": [
  13. ? ?? ?{ "sType": "html" },
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null,
  17. ? ?? ?null
  18. ? ? ]
  19. ??} );
  20. } );
sWidth:Defining the width of the column, this parameter may take any CSS value (3em, 20px etc). DataTables applies 'smart' widths to columns which have not been given a specific width through this interface ensuring that the table remains readable.
Default:null?Automatic
Type:string
  1. // Using aoColumnDefs
  2. $(document).ready( function() {
  3. ??$('#example').dataTable( {
  4. ? ? "aoColumnDefs": [
  5. ? ?? ?{ "sWidth": "20%", "aTargets": [ 0 ] }
  6. ? ? ]
  7. ??} );
  8. } );
  9. // Using aoColumns
  10. $(document).ready( function() {
  11. ??$('#example').dataTable( {
  12. ? ? "aoColumns": [
  13. ? ?? ?{ "sWidth": "20%" },
  14. ? ?? ?null,
  15. ? ?? ?null,
  16. ? ?? ?null,
  17. ? ?? ?null
  18. ? ? ]
  19. ??} );
  20. } );
三:回調
復制代碼
fnCookieCallback:還沒有使用過
$(document).ready( function () {$('#example').dataTable( {"fnCookieCallback": function (sName, oData, sExpires, sPath) {// Customise oData or sName or whatever else herereturn 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 {vars=(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 browsersif ( 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 = "";}} );
} );

轉載于:https://www.cnblogs.com/wynjauu/articles/9190962.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/397660.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397660.shtml
英文地址,請注明出處:http://en.pswp.cn/news/397660.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Tomcat配置JNDI數據源

經過3個多小時的努力&#xff0c;配置JNDI數據源(主要是通過DBCP連接池)終于搞定&#xff5e;還是Tomcat官方的說明好&#xff0c;不過全是英文的&#xff0c;大概還看得懂&#xff0e;百度上那么花花綠綠的太多了&#xff0c;一個也沒成功&#xff01;&#xff0e;&#xff0e…

java 線程池 固定大小_使用Executors服務在Java中創建固定大小線程池的最佳方法...

查看源代碼,您將意識到&#xff1a;Executors.newFixedThreadPool(threadPoolSize);相當于&#xff1a;return new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, MILLISECONDS,new LinkedBlockingQueue());由于它不提供顯式的RejectedExecutionHandler,因此使用默認…

令牌驗證 token

通過令牌驗證在注冊中心控制權限&#xff0c;以決定要不要下發令牌給消費者&#xff0c;可以防止消費者繞過注冊中心訪問提供者&#xff0c;另外通過注冊中心可靈活改變授權方式&#xff0c;而不需修改或升級提供者。 可以全局設置開啟令牌驗證&#xff1a; <!--隨機token令…

easybcd 支持 windows 10 和 ubuntu 14.04 雙系統啟動

家里計算機系統 windows 10 全新安裝。 原本是雙系統的&#xff0c;還有一個ubuntu。 windows 10 安裝以后&#xff0c;恢復ubuntu就是問題了。 (事后經驗&#xff1a;請不要立刻安裝bcd修改工具) 最初的方法是利用easybcd修改bcd記錄。操作是成功的&#xff0c;但系統重新啟動…

需求分析與原型設計

結對者&#xff1a;031402140李嚴 0314026617林瑞斌 需求分析與原型設計 NABCD模型 N&#xff08;Need&#xff0c;需求&#xff09;: 收集信息的過程太過繁瑣&#xff0c;有班級總負責人需匯總每一個同學的志愿并填入excel表中&#xff0c;上交年級負責人&#xff0c;年級負責…

java導出表格_java怎么導出excel表格

import com.spire.xls.ExcelVersion;import com.spire.xls.Workbook;import com.spire.xls.Worksheet;public class InsertArray {public static void main(String[] args) {//創建Workbook對象Workbook wb new Workbook();//獲取第一張62616964757a686964616fe4b893e5b19e313…

for 循環 和 Array 數組對象

博客地址&#xff1a;https://ainyi.com/12 for 循環 和 Array 數組對象方法 for for-in for-of forEach效率比較 - 四種循環&#xff0c;遍歷長度為 1000000 的數組疊加&#xff0c;得到的時間差&#xff1a;for 3for-in 250for-of 7forEach 44- 效率速度&#xff1a;for >…

IntelliJ IDEA---java的編譯工具【轉】

轉自&#xff1a;http://baike.baidu.com/link?urlsEpS0rItaB9BiO3i-qCdGSYiTIVPSJfBTjSXXngtN2hBhGl1j36CYQORKrbpqMHqjvu3MOfkgVzpMqr8To2l2q IDEA 全稱 IntelliJ IDEA&#xff0c;是java語言開發的集成環境&#xff0c;IntelliJ在業界被公認為最好的java開發工具之一&#…

OC中文件讀取類(NSFileHandle)介紹和常用使用方法

NSFileHandle 1.NSFileManager類主要對于文件的操作(刪除&#xff0c;修改&#xff0c;移動&#xff0c;賦值等等) //判斷是否有 tagetPath 文件路徑&#xff0c;沒有就創建NSFileManager *fileManage [NSFileManager defaultManager];BOOL success [fileManage createFileAt…

java filereader讀文件_Java FileReader讀文件

import java.io.*;class FileReaderDemo{public static void main(String[] args) throws IOException{//創建一個文件讀取流對象&#xff0c;和指定名稱的文件相關聯。//要保證該文件是已經存在的&#xff0c;如果不存在&#xff0c;會發生異常FileNotFoundExceptionFileReade…

struts2攔截器

struts攔截器 圖&#xff1a; 1、攔截器是什么&#xff1f; 分離關注&#xff1a; 完成一個功能&#xff0c;可以寫在一個類中&#xff0c;然后一個類中4個步驟&#xff0c;實現該類完成。 我們可以將4個步驟寫在4個類中&#xff0c;然后每一個類完成一部分功能&#xff0c;然后…

Springboot-Jpa多數據庫配置-2.0+版本

pom.xml增加: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency> 配置表同JdbcTemplate配置. 主數據源: ConfigurationEnableTransactionManagementEna…

Windows虛擬地址轉物理地址(原理+源碼實現,附簡單小工具)

By Lthis 上個月就想寫了&#xff0c;一直沒時間...網上大概搜了一下&#xff0c;原理與操作倒是一大堆&#xff0c;一直沒看到源碼實現&#xff0c;總得有人動手&#xff0c;這回輪到我了。東西寫得很爛&#xff0c;請大牛勿噴。一直覺得靠源碼的方式驅動學習是非常好的一種學…

python裝飾器的使用

借用裝飾器&#xff0c;我們可以批量的對老的函數進行改造或擴展老函數功能&#xff0c;比如需要對函數的接收參數進行過濾&#xff0c;Flash的url路由功能就是使用的這個方式 def dropoushu(): # 這一層函數可以去掉&#xff0c;如果去掉了&#xff0c;則使用checkjiou這種方…

7_文件上傳漏洞

文件上傳漏洞 當文件上傳時&#xff0c;若服務端腳本語言未對上傳的文件進行嚴格驗證和過濾&#xff0c;若惡意用戶上傳惡意的腳本文件時&#xff0c;就有可能控制整個網站甚至是服務器&#xff0c;這就是文件上傳漏洞。 權限 1.網站后臺權限&#xff1a;登陸了后臺&#xff0…

mysql數據庫如何實現分頁查詢_不同數據庫的分頁查詢實現方法總結

分頁查詢是數據庫查詢中經常用到的一項操作&#xff0c;對查詢出來的結果進行分頁查詢可以方便瀏覽。那么Oracle、SQL Server、MySQL是如何實現查詢的呢&#xff1f;本文我們就來介紹這一部分內容。首先我們先看一下SQL Server 數據庫中SQL語句查詢分頁數據的解決方案&#xff…

POJ - 3842 An Industrial Spy dfs(水)

題意:給你一串數字&#xff0c;最少一個&#xff0c;最多七個&#xff0c;問用這里面的數字能組成多少素數&#xff0c;不重復。 思路&#xff1a;之前還遍歷10000000的每一個素數&#xff0c;結果超時&#xff0c;后來發現直接dfs就可以了&#xff0c;只是標記一下做過的數。 …

飛機大戰小游戲1.0版本

小時候大家應該都玩過飛機大戰吧&#xff0c;這就是仿的一個飛機大戰&#xff0c;但是沒有寫的很全&#xff0c;只能玩一次&#xff0c;死掉之后需要刷新頁面玩第二次&#xff0c;話不說多&#xff0c;上代碼&#xff1a; 初始頁面&#xff1a; 整個的html代碼還是很少&#xf…

記一次Jquery獲取值的典型錯誤

直接上代碼&#xff1a; 代碼很簡單&#xff0c;通過Post的形式提交參數&#xff0c;但是發現提交的data總是空&#xff0c;昨晚有點納悶&#xff0c;今天一看才發現。。。 獲取值得時候的順序有問題&#xff0c;獲取值應該是在onclick事件中。 綜上&#xff1a;寫Jquery的時間…

android 調用java接口_android調用java的web service接口

android中通過webservice調用服務器端其實還是很簡單的&#xff0c;只要按部就班的按照下面步驟進行即可&#xff1a;(1)創建HttpTransportSE對象&#xff0c;該對象用于調用WebService操作代碼如下:HttpTransportSE ht new HttpTransportSE(SERVICE_URL);(2)創建SoapSerializ…