JqueryTable的使用

2021-7-7    前端达人

1.目的

用漂亮的前端表格直观显示数据

 

2. JqueryTable简介

JqueryTable官网

表格显示

 

3.用法

1.初始化html页面

设置一个table


  1. <table id="table_id" class="display">
  2. <thead>
  3. <tr>
  4. <th>Column 1</th>
  5. <th>Column 2</th>
  6. </tr>
  7. </thead>
  8. <tbody>
  9. <tr>
  10. <td>Row 1 Data 1</td>
  11. <td>Row 1 Data 2</td>
  12. </tr>
  13. <tr>
  14. <td>Row 2 Data 1</td>
  15. <td>Row 2 Data 2</td>
  16. </tr>
  17. </tbody>
  18. </table>

 

2.引入静态资源

这个是JqueryTable必要的静态资源


  1. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
  2. <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

如果你使用了js, 那么还需要引入jquery.js

 

3.初始化表格


  1. $(document).ready( function () {
  2. $('#table_id').DataTable();
  3. } );

3.实例

 

1. html页面


  1. <table id="tbl_grantedCoach" class="table table-striped table-bordered nowrap" style="table-layout:fixed; text-align: center;">
  2. <thead>
  3. <tr>
  4. <th width="10%">用户名</th>
  5. <th width="10%">姓名</th>
  6. <th width="8%">性别</th>
  7. <th width="10%">手机号</th>
  8. <th width="5%">工作年限</th>
  9. <th width="12%">教员类型</th>
  10. <th width="15%">角色</th>
  11. <th width="10%">备注</th>
  12. <th width="12%">操作</th>
  13. </tr>
  14. </thead>
  15. </table>

 

2.js代码

ajax 发送请求, 接受表格数据,再填写进去


  1. initGrantedCoachGrid:function(){
  2. selectedUserId =[];
  3. if(grantedCoachGrid){
  4. grantedCoachGrid.ajax.url(
  5. "course/getGrantedCoachByFolder?json&folderId="
  6. + selectNodeId + "").load();
  7. } else {
  8. grantedCoachGrid = $('#tbl_grantedCoach')
  9. .DataTable(
  10. {
  11. "lengthMenu" : [ [ 10, 20, 30 ],
  12. [ 10, 20, 30 ] // change per page
  13. // values here
  14. ],
  15. "ordering":false,
  16. "retrieve":true ,
  17. "bDestory" :true,
  18. "pageLength": 10,
  19. "bAutoWidth" : false,
  20. "ajax" : {
  21. "url" : "course/getGrantedCoachByFolder?json&folderId="
  22. + selectNodeId + "",
  23. "type" : "get",
  24. "cache" : false,
  25. "contentType" : "application/json; charset=utf-8",
  26. "dataSrc" : ""
  27. },
  28. "rowCallback" : function(row, data) {
  29. selectedUserId.push(data.userId);
  30. },
  31. "aoColumnDefs" : [ {
  32. sDefaultContent : '',
  33. aTargets : [ '_all' ]
  34. } ],
  35. // 填入列数据
  36. "columns" : [
  37. {
  38. "data" : "loginName"
  39. },
  40. {
  41. "data" : "userFullName"
  42. },
  43. {
  44. "data" : "gender",
  45. "mRender" : function(data,
  46. type, full) {
  47. if (data == "M") {
  48. return "男";
  49. } else if (data == "F") {
  50. return "女";
  51. }
  52. }
  53. },
  54. {
  55. "data" : "mobilePhone"
  56. },
  57. {
  58. "data" : "workYear"
  59. },
  60. { "data": "coachType" , "mRender":function(data,type,full){
  61. if(data=="0")
  62. {
  63. data="理论培训";
  64. }else if(data=="1"){
  65. data="实习培训";
  66. }else{
  67. data="理论培训+实习培训";
  68. }
  69. return data;
  70. }},
  71. {
  72. "data" :function( row, type, val, meta ){
  73. return row.userRoles[0].roleName;
  74. }
  75. },
  76. {
  77. "data" : "remark"
  78. },
  79. {
  80. "data" : null,
  81. "mRender" : function(data,
  82. type, full) {
  83. return "";
  84. }
  85. }
  86. ],
  87. "oLanguage" : {
  88. "sProcessing" : "正在加载中......",
  89. "sLengthMenu" : "每页显示_MENU_条记录",
  90. "sZeroRecords" : "对不起,查询不到相关数据!",
  91. "sEmptyTable" : "无数据存在!",
  92. "sInfo" : "当前显示_START_到_END_条,共_TOTAL_条记录",
  93. "sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
  94. "sSearch" : "",
  95. "oPaginate" : {
  96. "sFirst" : "首页",
  97. "sPrevious" : "上一页",
  98. "sNext" : "下一页",
  99. "sLast" : "末页"
  100. }
  101. }, // 多语言配置
  102. "stateSave" : true
  103. // save the state of a table
  104. });
  105. }
  106. }

蓝蓝设计建立了UI设计分享群,每天会分享国内外的一些优秀设计,如果有兴趣的话,可以进入一起成长学习,请扫码蓝小助,报下信息,蓝小助会请您入群。欢迎您加入噢~~希望得到建议咨询、商务合作,也请与我们联系。

截屏2021-05-13 上午11.41.03.png


分享此文一切功德,皆悉回向给文章原作者及众读者.

免责声明:蓝蓝设计尊重原作者,文章的版权归原作者。如涉及版权问题,请及时与我们取得联系,我们立即更正或删除。

蓝蓝设计www.lanlanwork.com )是一家专注而深入的界面设计公司,为期望卓越的国内外企业提供卓越的UI界面设计、BS界面设计 、 cs界面设计 、 ipad界面设计 、 包装设计 、 图标定制 、 用户体验 、交互设计、 网站建设 平面设计服务

分享本文至:

日历

链接

blogger

蓝蓝 http://www.lanlanwork.com

存档