dynamicColumns.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!DOCTYPE html>
  2. <html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
  3. <head>
  4. <th:block th:include="include :: header('动态列')" />
  5. </head>
  6. <body class="gray-bg">
  7. <div class="container-div">
  8. <div class="row">
  9. <div class="col-sm-12 search-collapse">
  10. <form id="table-form">
  11. <div class="select-list">
  12. <ul>
  13. <li>
  14. 要增加的列:
  15. <select name="field">
  16. <option value="">默认</option>
  17. <option value="userBalance">用户余额</option>
  18. </select>
  19. </li>
  20. <li>
  21. <a class="btn btn-primary btn-rounded btn-sm" onclick="tableSearch()"><i class="fa fa-search"></i>&nbsp;搜索</a>
  22. <a class="btn btn-warning btn-rounded btn-sm" onclick="reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
  23. </li>
  24. </ul>
  25. </div>
  26. </form>
  27. </div>
  28. <div class="col-sm-12 select-table table-striped">
  29. <table id="bootstrap-table"></table>
  30. </div>
  31. </div>
  32. </div>
  33. <div th:include="include :: footer"></div>
  34. <script th:inline="javascript">
  35. var prefix = ctx + "demo/table";
  36. var datas = [[${@dict.getType('sys_normal_disable')}]];
  37. $(function() {
  38. ajaxColumns();
  39. });
  40. // 动态获取列
  41. function ajaxColumns() {
  42. var url = prefix + "/ajaxColumns";
  43. var dataParam = $.common.formToJSON("table-form");
  44. $.modal.loading("正在查询中,请稍后...");
  45. $.post(url, dataParam, function(result) {
  46. if (result.code == web_status.SUCCESS) {
  47. setColumns(result.data);
  48. } else if (result.code == web_status.WARNING) {
  49. $.modal.alertWarning(result.msg)
  50. } else {
  51. $.modal.alertError(result.msg);
  52. }
  53. $.modal.closeLoading();
  54. });
  55. }
  56. // 设置列
  57. function setColumns(list) {
  58. var columns = [];
  59. list.forEach(function(item) {
  60. if($.common.equals('status',item.field)){
  61. columns.push({
  62. field : item.field,
  63. title : item.title,
  64. align: 'center',
  65. formatter: function(value, row, index) {
  66. return $.table.selectDictLabel(datas, value);
  67. }
  68. })
  69. } else {
  70. columns.push({
  71. field : item.field,
  72. title : item.title
  73. })
  74. }
  75. });
  76. columns.push({
  77. title: '操作',
  78. align: 'center',
  79. formatter: function(value, row, index) {
  80. var actions = [];
  81. actions.push('<a class="btn btn-success btn-xs" href="#"><i class="fa fa-edit"></i>编辑</a> ');
  82. actions.push('<a class="btn btn-danger btn-xs" href="#"><i class="fa fa-remove"></i>删除</a>');
  83. return actions.join('');
  84. }
  85. });
  86. if(!table.get(table.options.id)){
  87. initTable(columns);
  88. } else {
  89. refreshTable(columns);
  90. }
  91. }
  92. // 刷新表格
  93. function refreshTable(columns) {
  94. var options = {
  95. columns: columns
  96. };
  97. $("#" + table.options.id).bootstrapTable('refreshOptions',options);
  98. }
  99. // 初始化表格
  100. function initTable(columns){
  101. var options = {
  102. url: prefix + "/list",
  103. showSearch: false,
  104. showRefresh: false,
  105. showToggle: false,
  106. showColumns: false,
  107. columns: columns
  108. };
  109. $.table.init(options);
  110. }
  111. // 搜索
  112. function tableSearch(){
  113. ajaxColumns();
  114. }
  115. // 重置
  116. function reset(){
  117. $("#table-form")[0].reset();
  118. ajaxColumns();
  119. }
  120. </script>
  121. </body>
  122. </html>