與EasyUI的Datagrid做用同樣,比easyui更漂亮javascript
Datatables是一款jquery表格插件。它是一個高度靈活的工具,能夠將任何HTML表格添加高級的交互功能。css
支持分頁、排序、搜索java
支持4種數據源jquery
支持多種主題ajax
擁有多種擴展json
至少引入以下3個文件數組
<link rel="stylesheet" href="css/jquery.dataTables.min.css" /> <script type="text/javascript" src="js/jquery.js" ></script> <script type="text/javascript" src="js/jquery.dataTables.min.js" ></script>
Bootstrap 3瀏覽器
Foundation服務器
Semantic UIapp
jQuery UI
Base
Bootstrap 4
導入的文件不一樣,具體請看官網示例:https://www.datatables.net
<table id="example" cellspacing="0" width="100%"> <thead> <tr> <th>item1</th> <th>item1</th> <th>item1</th> </tr> </thead> <tbody> <tr> <td>item1</td> <td>item1</td> <td>item1</td> </tr> </tbody> <tfoot> <tr> <th></th> <th></th> <th></th> </tr> </tfoot> </table>
想使用DataTables表格插件,至少要寫<table></table>
標籤,而後再經過js渲染
默認初始化
$('#example').DataTable();
配置初始化
$('#example').DataTable({ "scrollY" : 350, "ajax" : 'http://wt.com', "serverSide" : true });
經常使用配置項
info //是否顯示左下角信息 ordering //是否開啓排序 paging //是否開啓分頁 searching //是否開啓查詢 serverSide ajax data lengthMenu: [ 10, 25, 50, 75, 100 ] //定義每頁顯示記錄數
HTML(DOM)數據源——後臺模板拼接
<table id="example" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </thead> <tfoot> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </tfoot> <tbody> <tr> <td>Tiger Nixon</td> <td>System Architect</td> <td>Edinburgh</td> <td>61</td> <td>2011/04/25</td> <td>$320,800</td> </tr> <tr> <td>Cedric Kelly</td> <td>Senior Javascript Developer</td> <td>Edinburgh</td> <td>22</td> <td>2012/03/29</td> <td>$433,060</td> </tr> </tbody> </table>
Javascript數據源(Array/Objects)——優先級比HTMLDOM高
有2種類型: 二維數組: var dataSet = [ [ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ], [ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ] ]; 對象數組(必須配合columns配置項): var dataSet = [ { "name": "Tiger Nixon", "position": "System Architect", "salary": "$3,120", "start_date": "2011/04/25", "office": "Edinburgh", "extn": "5421" }, { "name": "Garrett Winters", "position": "Director", "salary": "$5,300", "start_date": "2011/07/25", "office": "Edinburgh", "extn": "8422" } ]; $(document).ready(function() { $('#example').DataTable( { data: dataSet, columns: [ { title: "Name" }, { title: "Position" }, { title: "Office" }, { title: "Extn." }, { title: "Start date" }, { title: "Salary" } ] } ); } );
Ajax with client-side processing數據源
服務器返回的數據格式必須是: { "data": [ [ "Howard Hatfield", "Office Manager", "San Francisco", "7031", "2008/12/16", "$164,500" ], [ "Hope Fuentes", "Secretary", "San Francisco", "6318", "2010/02/12", "$109,850" ] ] } 或者 { "data": [ { "name": "Tiger Nixon", "position": "System Architect", "salary": "$3,120", "start_date": "2011/04/25", "office": "Edinburgh", "extn": "5421" }, { "name": "Garrett Winters", "position": "Director", "salary": "$5,300", "start_date": "2011/07/25", "office": "Edinburgh", "extn": "8422" } ] } $(document).ready(function() { $('#example').DataTable( { "ajax": '../ajax/data/arrays.txt' } ); } ); 或 $(document).ready(function() { $('#example').DataTable( { "ajax": { "url": "data.json", "data": { "user_id": 451 } } } ); } );
Ajax with server-side processing數據源
服務器返回的數據格式: { "draw" : 1, "recordsTotal" : 20, "recordsFiltered" : 20, "data" : [ [],[] ] }
在DataTables表格初始化的時候進行初始化,使用columns
或者columnDefs
屬性進行自定義列的信息
能自定義列的標題、顯示內容、樣式、別名、數據綁定、是否提供排序、是否提供搜索過濾、列寬、默認內容等等
示例
$('#example').DataTable({ "ajax" : '{:U("getList")}', "serverSide" : true, "columns": [ { "searchable": false, "name": "engine", "title" : "wutao", "orderable": false, "className": "my_class", "render": function ( data, type, full, meta ) { return '<a href="'+data+'">Download</a>'; } }, null ] });
當使用服務器模式"serverSide" : true
時,瀏覽器會發出一個GET請求來獲取數據源
請求的查詢參數以下:
draw:1 //請求次數,用於響應是也要帶回來 columns[0][data]:0 //第一列綁定的數據源標識,二維數組就是數字,對象數組就是key columns[0][name]:engine //第一列別名 columns[0][searchable]:false //不可搜索 columns[0][orderable]:true //不可排序 columns[0][search][value]: //搜索的條件 columns[0][search][regex]:false //搜索是否使用正則 ..... //有多少列就有多少個columns[n] order[0][column]:0 //指定排序的列 order[0][dir]:asc //指定列的排序方式 start:0 //起始下標 length:10 //每頁記錄數 search[value]: //全局搜索條件 search[regex]:false //全局搜索是否使用正則 _:1492657609627 //自帶標示不用理會
在DataTables表格初始化時,使用language
屬性對錶格中的文字信息進行靈活修改
示例:
$('#example').dataTable( { "language": { "processing": "DataTables is currently busy", "emptyTable": "No data available in table", "info": "Showing page _PAGE_ of _PAGES_", "lengthMenu": "每頁顯示 _MENU_ 條記錄", "search": "搜索:" } } );
列表項目
在DataTables表格控件初始化時,使用dom
屬性和initComplete
回調函數來統一配置
應用場景:把自定義按鈕集成到DataTables上面
$('#example').dataTable( { "dom": "l<'#customid'>ftip", "initComplete": function(){ $("#customid").append("<button></button>"); } } );
自定義表格DOM最好把柵格加進去
$('#example').dataTable( { "dom": "<'.row'<'#customid.col-xs-4'><'.col-xs-8'f>><'.row'<'.col-xs-12't>>", "initComplete": function(){ $("#customid").append("<button></button>"); } } );
drawCallback比initComplete優先執行
Html結構
<input type="checkbox" class="i-checks" id="checkAll"> //表頭
JS部分
$('#example').DataTable({ "ajax" : '{:U("getList")}', "serverSide" : true, "columns": [ { "render": function ( data, type, row, meta ) { return '<input type="checkbox" class="i-checks item" name="ids[]" value="'+row.id+'">'; } }, null ], "drawCallback": function(){ checkbox_init(); } }); //全選,全不選 function checkbox_init(){ $(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",}) $('#checkAll').on('ifChecked', function(event){ $(this).off('ifUnchecked'); $('.item').iCheck('check'); $(this).on('ifUnchecked', function(event){ $('.item').iCheck('uncheck'); }) }); $('.item').on('ifUnchecked',function(event){ $('#checkAll').off('ifUnchecked'); $('#checkAll').iCheck('uncheck'); }).on('ifChecked',function(event){ var state = true; $('.item').each(function(i){ if(!$(this).is(':checked')){ state = false; } }); if(state){ $('#checkAll').iCheck('check'); } }); }