Jquery dataTables api 配置筆記

轉自:http://blog.csdn.net/zhulongxi/article/details/52301547

一、詳細配置:

  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.         }  
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>');
        }


二、得到某航的值:

需要引入 jQuery.dataTables.nightly.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. } );   
$(document).ready(function() {  
    /* Init DataTables */  
    $('#example').dataTable();  
$('#example tbody tr').live('click', function () {  
                var sTitle;  
                var nTds = $('td', this);  
                var sday = $(nTds[0]).text();  //得到第1列的值  
                var sout = $(nTds[1]).text();  
                var sin = $(nTds[2]).text();  
                  
            } );  
  
} ); 

三、設置隱藏列:

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

四、得到當前頁中的數據:

1.得到所有數據:

  1. var alldata=$('#example').dataTable().fnGetData();//得到頁面中所有對象  
var alldata=$('#example').dataTable().fnGetData();//得到頁面中所有對象
2.得到某行數據:

  1. // Row data   
  2. $(document).ready(function() {   
  3. oTable = $('#example').dataTable();   
  4.   
  5. oTable.$('tr').click( function () {   
  6. var data = oTable.fnGetData( this );   
  7. // ... do something with the array / object of data for the row   
  8. } );   
  9. } );   
// 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 
} ); 
} ); 

3.得到單個單元格的數據:

  1. // Individual cell data   
  2. $(document).ready(function() {   
  3. oTable = $('#example').dataTable();   
  4.   
  5. oTable.$('td').click( function () {   
  6. var sData = oTable.fnGetData( this );   
  7. alert( 'The cell clicked on had the value of '+sData );   
  8. } );   
  9. } );  
// 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 ); 
} ); 
} );

4.刷新表中的數據:最後一個參數表示是否重繪表格,false表示你瀏覽到第二頁時刷新後不會刷跑到第一頁,但數據不會改變

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

  1. $('#example').dataTable().fnUpdate( 'a' , 1 , 1 ,false); //coll   
  2. $('#example').dataTable().fnUpdate( ['a','b'] , 1 ); //row  
$('#example').dataTable().fnUpdate( 'a' , 1 , 1 ,false); //coll 
$('#example').dataTable().fnUpdate( ['a','b'] , 1 ); //row

4.得到當前瀏覽的datatables頁碼

  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. }    
    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跳轉到某頁

  1. $('#example').dataTable().fnPageChange(page);    
$('#example').dataTable().fnPageChange(page);  

六、Dom

  1. http://datatables.net/release-datatables/examples/api/select_single_row.html 選擇一行  
  2. http://datatables.net/release-datatables/examples/api/select_row.html選擇多行  
  3. http://datatables.net/release-datatables/examples/api/editable.html 即時編輯行  
  4. http://datatables.net/release-datatables/examples/api/tabs_and_scrolling.html 多個tab  
  5. http://datatables.net/release-datatables/examples/api/add_row.html添加一行(靜態)  
  6. http://datatables.net/release-datatables/examples/api/multi_filter.html多列搜索  
  7. http://datatables.net/release-datatables/examples/api/multi_filter_select.html多列搜索(擴展)  
  8. http://datatables.net/release-datatables/examples/api/highlight.html行列 高亮  
  9. http://datatables.net/release-datatables/examples/advanced_init/highlight.html 鼠標移上去亮  
  10. http://datatables.net/release-datatables/examples/api/row_details.html行詳細信息  
  11. http://datatables.net/release-datatables/examples/api/counter_column.html添加行數  
  12. http://datatables.net/release-datatables/examples/api/show_hide.html隱藏列  
  13. http://datatables.net/release-datatables/examples/api/api_in_init.html點中即爲搜索條件  
  14. http://datatables.net/release-datatables/examples/advanced_init/events_live.html 給每一個行添加事件顯示行的信息  
  15. http://datatables.net/release-datatables/examples/advanced_init/events_pre_init.html鼠標事件 移到某一行顯示信息  
  16. http://datatables.net/release-datatables/examples/advanced_init/events_post_init.html 同上  
  17. http://datatables.net/release-datatables/examples/advanced_init/dom_multiple_elements.html 用sdom屬性控制插件位置  
  18. http://datatables.net/release-datatables/examples/advanced_init/dom_toolbar.htmlsdom應用  
  19. http://datatables.net/release-datatables/examples/advanced_init/length_menu.html更改按多少數據顯示  
  20. http://datatables.net/release-datatables/examples/advanced_init/complex_header.html表頭組  
  21. http://datatables.net/release-datatables/examples/advanced_init/row_grouping.html按組顯示行  
  22. http://datatables.net/release-datatables/examples/advanced_init/row_callback.html列回調函數  
  23. http://datatables.net/release-datatables/examples/advanced_init/footer_callback.html總計(footer回調)  
  24. http://datatables.net/release-datatables/examples/advanced_init/sorting_control.html自定義排序  
  25. http://datatables.net/release-datatables/examples/advanced_init/language_file.html國際化  
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國際化

七、配置參數:

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 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 = "";
    }
  } );
} );

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章