溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

了解DataTables

發(fā)布時間:2020-08-03 20:38:12 來源:網(wǎng)絡(luò) 閱讀:673 作者:qwerpou 欄目:web開發(fā)

官方網(wǎng)站:http://www.datatables.net/

簡介:DataTables是一個jQuery的表格插件。這是一個高度靈活的工具,依據(jù)的基礎(chǔ)逐步增強,這將增加先進的互動控制,支持任何HTML表格。

主要特點:

 

  • 自動分頁處理

  • 即時表格數(shù)據(jù)過濾

  • 數(shù)據(jù)排序以及數(shù)據(jù)類型自動檢測

  • 自動處理列寬度

  • 可通過CSS定制樣式

  • 支持隱藏列

  • 易用

  • 可擴展性和靈活性

  • 國際化

  • 動態(tài)創(chuàng)建表格

  • 免費的 

參數(shù)初始化:

 

bAutoWidth 

 

是否啟用自動適應(yīng)列寬

默認(rèn)值:True

示例代碼: 

$(document).ready(function(){
  $('#example').dataTable({
    "bAutoWidth": false
  });
});


 bDeferRender 

 是否啟用延遲加載:當(dāng)你使用AJAX數(shù)據(jù)源時,可以提升速度。

 默認(rèn)值:False

示例代碼:

$(document).ready(function(){
  var oTable = $('#example').dataTable({
    "sAjaxSource":"sources/arrays.txt",
    "bDeferRender":true
  });
});


bFilter 

是否啟用內(nèi)置搜索功能:可以跨列搜索。

默認(rèn)值:True 

示例代碼:

 $(document).ready(function(){
  $('#example').dataTable({
    "bFilter":false
  });
});

 

bInfo  

 是否顯示表格相關(guān)信息:例如翻頁信息等。

默認(rèn)值:True 

示例代碼: 

$(document).ready(function(){
  $('#example').dataTable({
    "bInfo":false
  });
});

 

bJQueryUI

是否啟用  jQuery UI 皮膚插件支持

默認(rèn)值:False

示例代碼: 

$(document).ready(function(){
  $('#example').dataTable({
    "bJQueryUI":true
  });
});


 bLengthChange

是否允許用戶,在下拉列表自定義選擇分頁大小(10, 25, 50 and 100),需要分頁支持

默認(rèn)值:True

示例代碼: 

 $(document).ready(function(){
  $('#example').dataTable({
    "bLengthChange": false
  });
});

 

bPaginate 

是否開啟分頁

默認(rèn)值:Ture

示例代碼:

 $(document).ready(function (){ 
  $('#example').dataTable({
    "bPaginate":false
  });
});


bProcessing 

 是否啟用進度顯示,進度條等等,對處理大量數(shù)據(jù)很有用處。

默認(rèn)值:false

示例代碼: 

$(document).ready(function(){
  $('#example').dataTable({
    "bProcessing":true
  });
});

 

bScrollInfinite  

是否開啟內(nèi)置滾動條,并且顯示所有數(shù)據(jù)

默認(rèn)值:fasle 

 示例代碼: 

$(document).ready(function(){
  $('#example').dataTable({
    "bScrollInfinite":true,
    "bScrollCollapse":true,
    "sScrollY": "200px"
  });
});


 bServerSide

是否啟用服務(wù)器處理數(shù)據(jù)源,必須sAjaxSource指明數(shù)據(jù)源位置

默認(rèn)值:False

示例代碼:

$(document).ready(function(){
  $('#example').dataTable({
    "bServerSide":true,
    "sAjaxSource":"xhr.php"
  });
});


 bSort 

是否開啟列排序功能,如果想禁用某一列排序,可以在每列設(shè)置使用bSortable參數(shù)

 默認(rèn)值:True

示例代碼: 

$(document).ready(function(){
  $('#example').dataTable({
    "bSort":false
  });
});


bSortClasses  

 開關(guān),指定當(dāng)當(dāng)前列在排序時,是否增加classes ‘sorting_1′, ‘sorting_2′ and ‘sorting_3’,打開后,在處理大數(shù)據(jù)時,性能有所損失

 默認(rèn)值:False

示例代碼:

$(document).ready(function(){
  $('#example').dataTable({
    "bSortClasses":false
  });
});


bStateSave  

 是否開啟cookies保存當(dāng)前狀態(tài)信息

 默認(rèn)值:false

 示例代碼:

$(document).ready(function(){
  $('#example').dataTable({
    "bStateSave": true
  });
});


sScrollX 

是否開啟水平滾動條

默認(rèn)值:空 (字符串類型)

 示例代碼:

$(document).ready(function(){
  $('#example').dataTable({
    "sScrollX":"100%",
    "bScrollCollapse": true
  });
});


sScrollY

 是否開啟垂直滾動條,滾動條大小受限于Datatable的高度大小

默認(rèn)值:空(字符串類型)

示例代碼:

$(document).ready(function(){
  $('#example').dataTable({
    "sScrollY":"200px",
    "bPaginate":false
  });
});


屬性名稱取值范圍解釋
bAutoWidthtrue or false, default true是否自動計算表格各列寬度
bDeferRendertrue or false, default false用于渲染的一個參數(shù)
bFiltertrue or false, default true開關(guān),是否啟用客戶端過濾功能
bInfotrue or false, default true開關(guān),是否顯示表格的一些信息
bJQueryUItrue or false, default false是否使用jquery ui themeroller的風(fēng)格
bLengthChangetrue or false, default true開關(guān),是否顯示一個每頁長度的選擇條(需要分頁器支持)
bPaginatetrue or false, default true開關(guān),是否顯示(使用)分頁器
bProcessingtrue or false, defualt false開關(guān),以指定當(dāng)正在處理數(shù)據(jù)的時候,是否顯示“正在處理”這個提示信息
bScrollInfinitetrue or false, default false開關(guān),以指定是否無限滾動(與sScrollY配合使用),在大數(shù)據(jù)量的時候很有用。當(dāng)這個標(biāo)志為true的時候,分頁器就默認(rèn)關(guān)閉
bSorttrue or false, default true開關(guān),是否讓各列具有按列排序功能
bSortClassestrue or false, default true開關(guān),指定當(dāng)當(dāng)前列在排序時,是否增加classes ‘sorting_1′, ‘sorting_2′ and ‘sorting_3’,打開后,在處理大數(shù)據(jù)時,性能有所損失
bStateSavetrue or false, default false開關(guān),是否打開客戶端狀態(tài)記錄功能。這個數(shù)據(jù)是記錄在cookies中的,打開了這個記錄后,即使刷新一次頁面,或重新打開瀏覽器,之前的狀態(tài)都是保存下來的
sScrollX‘disabled’ or  ‘100%’ 類似的字符串是否開啟水平滾動,以及指定滾動區(qū)域大小
sScrollY‘disabled’ or ‘200px’ 類似的字符串是否開啟垂直滾動,以及指定滾動區(qū)域大小
選項

aaSortingarray array[int,string], 如[], [[0,’asc’], [0,’desc’]]指定按多列數(shù)據(jù)排序的依據(jù)
aaSortingFixed同上同上。唯一不同點是不能被用戶的自定義配置沖突
aLengthMenudefault [10, 25, 50, 100],可以為一維數(shù)組,也可為二維數(shù)組,比如:[[10, 25, 50, -1], [10, 25, 50, “All”]]這個為選擇每頁的條目數(shù),當(dāng)使用一個二維數(shù)組時,二維層面只能有兩個元素,第一個為顯示每頁條目數(shù)的選項,第二個是關(guān)于這些選項的解釋
aoSearchColsdefault null, 類似:[null, {“sSearch”: “My filter”}, null,{“sSearch”: “^[0-9]”, “bEscapeRegex”: false}]給每個列單獨定義其初始化搜索列表特性(這一塊還沒搞懂)
asStripClassesdefault [‘odd’, ‘even’], 比如[‘strip1′, ‘strip2′, ‘strip3′]指定要被應(yīng)用到各行的class風(fēng)格,會自動循環(huán)
bDestroytrue or false, default false用于當(dāng)要在同一個元素上執(zhí)行新的dataTable綁定時,將之前的那個數(shù)據(jù)對象清除掉,換以新的對象設(shè)置
bRetrievetrue or false, default false用于指明當(dāng)執(zhí)行dataTable綁定時,是否返回DataTable對象
bScrollCollapsetrue or false, default false指定適當(dāng)?shù)臅r候縮起滾動視圖
bSortCellsToptrue or false, default false(未知的東東)
iCookieDuration整數(shù),默認(rèn)7200,單位為秒指定用于存儲客戶端信息到cookie中的時間長度,超過這個時間后,自動過期
iDeferLoading整數(shù),默認(rèn)為null延遲加載,它的參數(shù)為要加載條目的數(shù)目,通常與bServerSide,sAjaxSource等配合使用
iDisplayLength整數(shù),默認(rèn)為10用于指定一屏顯示的條數(shù),需開啟分頁器
iDisplayStart整數(shù),默認(rèn)為0用于指定從哪一條數(shù)據(jù)開始顯示到表格中去
iScrollLoadGap整數(shù),默認(rèn)為100用于指定當(dāng)DataTable設(shè)置為滾動時,最多可以一屏顯示多少條數(shù)據(jù)
oSearch默認(rèn){ “sSearch”: “”, “bRegex”: false, “bSmart”: true }又是初始時指定搜索參數(shù)相關(guān)的,有點復(fù)雜,沒搞懂目前
sAjaxDataProp字符串,default ‘a(chǎn)aData’指定當(dāng)從服務(wù)端獲取表格數(shù)據(jù)時,數(shù)據(jù)項使用的名字
sAjaxSourceURL字符串,default null指定要從哪個URL獲取數(shù)據(jù)
sCookiePrefix字符串,default ‘SpryMedia_DataTables_’當(dāng)打開狀態(tài)存儲特性后,用于指定存儲在cookies中的字符串的前綴名字
sDomdefault 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’用于指定分頁器風(fēng)格
sScrollXInnerstring default ‘disabled’又是水平滾動相關(guān)的,沒搞懂啥意思


回調(diào)函數(shù)名稱參數(shù)返回值默認(rèn)功能
fnCookieCallback1.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當(dāng)每次cookies改變時,會觸發(fā)這個函數(shù)調(diào)用
fnDrawCallback在每次table被draw完后調(diào)用,至于做什么就看著辦吧
fnFooterCallback1.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
用于在每次重畫的時候修改表格的腳部
fnFormatNumber1.int : number to be formattedString : formatted string for DataTables to show the number有默認(rèn)的用于在大數(shù)字上,自動加入一些逗號,分隔開
fnHeaderCallback1.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發(fā)生時,修改table的header
fnInfoCallback1.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.用于傳達(dá)table信息
fnInitComplete1.object:oSettings – DataTables settings object表格初始化完成后調(diào)用
fnPreDrawCallback1.object:oSettings – DataTables settings objectBoolean用于在開始繪制之前調(diào)用,返回false的話,會阻止draw事件發(fā)生;返回其它值,draw可以順利執(zhí)行
fnRowCallback1.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當(dāng)創(chuàng)建了行,但還未繪制到屏幕上的時候調(diào)用,通常用于改變行的class風(fēng)格
fnServerData1.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用于替換默認(rèn)發(fā)到服務(wù)端的請求操作
fnStateLoadCallback1.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中的數(shù)據(jù)被加載前執(zhí)行,可以方便地修改這些數(shù)據(jù)
fnStateSaveCallback1.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在狀態(tài)數(shù)據(jù)被存儲到cookies前執(zhí)行,可以方便地做一些預(yù)操作



具體的參數(shù)意思可以參考官方文檔。這里提一下fnServerParams。這個參數(shù)可以幫我們傳遞頁面的其他參數(shù),這個例子中,我傳遞了關(guān)鍵字、起止日期信息,主要用于服務(wù)端的數(shù)據(jù)查找過濾。


用chrome看一下控件會向服務(wù)端傳遞什么:

了解DataTables


這是底部的分頁欄

了解DataTables


當(dāng)我點第二頁時,傳遞參數(shù)如下:

了解DataTables

注意到iDisplayLength10,因為我默認(rèn)是每頁展示10條數(shù)據(jù),這個值可以調(diào)整。當(dāng)我點第二頁時,iDisplayStart0變到10。其實分頁就是這么回事,前端告訴服務(wù)端,我需要那段數(shù)據(jù),你給我找出來返回給我。


服務(wù)端的返回格式如下(PHP):

$ret = array(
                "sEcho" => intval($_GET['sEcho']),
                "iTotalRecords" => $data_count,
                "iTotalDisplayRecords" => $data_count,
                "aaData" => array()
            );


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI