jquery datatables 表格插件
之前用的bootstrap的插件做动态搜索,比较麻烦,这几天写个小项目用到动态搜索,找到jquery的datatables插件,很好用。
中文官网地址:http://www.datatables.club/
英文官网地址:https://www.datatables.net/
如下是简单的应用,支持动态搜索。以及排序。
导入js:
1 <link rel="stylesheet" href="/static/trouble_run/css/jquery.dataTables.min.css "> 2 <link rel="stylesheet" href="/static/trouble_run/css/shCore.css "> 3 <script src="/static/trouble_run/js/jquery-2.1.1.min.js"></script> 4 <script src="/static/trouble_run/js/jquery.dataTables.min.js"></script>
数据获取,一般咱们采用 ajax 从后台db获取数据。
ajax:
$.ajax({ type:'post',//可选get url:'/query_task/', data:{'id':id}, dataType:'JSON',//服务器返回的数据类型 可选XML ,Json jsonp script htmltext等 success:function(msg){ var msgObj=msg; //重新构建table $('#example').dataTable().fnClearTable(); //将数据清除 $('#example').dataTable().fnAddData(packagingdatatabledata(msgObj),true); //数据必须是json对象或json对象数组 }, error:function(){ alert('error'); } })})
因为该插件需要数据的类型为元组,所以需要函数进行处理如下:
1 function packagingdatatabledata(msgObj){ 2 3 var editHtml="<a class='btn' data-toggle='modal' href='#modalbackdroptrue' >编辑</a>"; 4 var a=[]; 5 $.each(msgObj,function(k,key){ 6 var tempObj=[]; 7 8 tempObj.push(key['id']); 9 tempObj.push(key['ip']); 10 a.push(tempObj) 11 } 12 ) 13 return a; 14 15 16 }
html:
1 <table id="example" class="display table-bordered" cellspacing="0" width="100%"> 2 <thead> 3 <tr> 4 <th>xx</th> 5 <th>xx</th> 6 7 </tr> 8 </thead> 9 </table>
只需要后台传入生成json串,传到前端即可,由于不同的语言开发的不一样,不在表述。
效果:

支持排序分页。
更多配置可以去官网查看。
学习是一种态度,坚持是质变的利器!

浙公网安备 33010602011771号