col_set.ejs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!--弹出列设置-->
  2. <div class="modal fade" id="col-set" data-backdrop="static">
  3. <div class="modal-dialog" role="document">
  4. <div class="modal-content">
  5. <div class="modal-header">
  6. <h5 class="modal-title">列设置</h5>
  7. </div>
  8. <div class="modal-body">
  9. <table class="table table-hover table-bordered">
  10. <thead><tr><th width="150px" class="text-center">列名</th><th width="50px" class="text-center">显示</th><th width="150px" class="text-center">别名</th><th width="80px" class="text-center">操作</th></tr></thead>
  11. <tbody class="text-center">
  12. <% for (const cs of colSet) { %>
  13. <tr code="<%- cs.field %>">
  14. <td>
  15. <%- cs.name %>
  16. <% if (cs.hint) { %>
  17. <i class="fa fa-question-circle text-primary ml-1" data-placement="bottom" data-toggle="tooltip" data-original-title="<%- cs.hint %>"></i>
  18. <% } %>
  19. </td>
  20. <% if (cs.fixed.indexOf('show') >= 0 ) { %>
  21. <td>
  22. <div class="form-check form-check-inline">
  23. <input class="form-check-input" type="checkbox" id="inlineCheckbox-<%- cs.code %>" checked="" disabled="">
  24. <label class="form-check-label" for="inlineCheckbox-<%- cs.code %>"></label>
  25. </div>
  26. </td>
  27. <% } else { %>
  28. <td>
  29. <div class="form-check form-check-inline">
  30. <input class="form-check-input" type="checkbox" id="inlineCheckbox-<%- cs.code %>" <% if (cs.show) { %> checked <% } %> >
  31. <label class="form-check-label" for="inlineCheckbox-<%- cs.code %>"></label>
  32. </div>
  33. </td>
  34. <% } %>
  35. <% if (cs.fixed.indexOf('alias') >= 0) { %>
  36. <td class="disabled">-</td>
  37. <% } else {%>
  38. <td><input type="text" class="form-control form-control-sm" value="<%- cs.alias %>"></td>
  39. <% } %>
  40. <td>
  41. <% if (!cs.gd) { %>
  42. <a href="javascript:;" class="move-up text-primary mr-2" style="text-decoration: none;">上移</a>
  43. <a href="javascript:;" class="move-down text-primary" style="text-decoration: none;">下移</a>
  44. <% } %>
  45. </td>
  46. </tr>
  47. <% } %>
  48. </tbody>
  49. </table>
  50. </div>
  51. <form class="modal-footer" action="/sp/<%- ctx.subProject.id %>/contract/<%- ctx.contractOptions.tid ? 'tender/'+ctx.contractOptions.tid + '/' : '' %>col-set" method="post" onsubmit="return onSetCol();">
  52. <input type="hidden" name="_csrf_j" value="<%= ctx.csrf %>">
  53. <input type="hidden" name="col_set" value="">
  54. <input type="hidden" name="type" value="<%- ctx.contract_type %>">
  55. <input type="hidden" name="col_type" value="info">
  56. <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">关闭</button>
  57. <button type="submit" class="btn btn-primary btn-sm" id="add-bd-ok">确定修改</button>
  58. </form>
  59. </div>
  60. </div>
  61. </div>
  62. <script>
  63. // 立即获取表格 tbody 并绑定事件(确保脚本加载时即可初始化)
  64. const $tbody = $('#col-set').find('.modal-body table tbody');
  65. // 保存初始 HTML,在关闭时恢复(保证再次打开时为初始状态)
  66. const _initialColSetTbodyHtml = $tbody.html();
  67. // 更新上下移链接显示状态
  68. function updateMoveButtons() {
  69. const $currentRows = $tbody.find('tr');
  70. const totalRows = $currentRows.length;
  71. $currentRows.each(function(index) {
  72. const $row = $(this);
  73. const $upLink = $row.find('.move-up');
  74. const $downLink = $row.find('.move-down');
  75. if (totalRows === 1) {
  76. $upLink.hide();
  77. $downLink.hide();
  78. } else if (index === 0) {
  79. $upLink.hide();
  80. $downLink.show();
  81. } else if (index === totalRows - 1) {
  82. $upLink.show();
  83. $downLink.hide();
  84. } else {
  85. $upLink.show();
  86. $downLink.show();
  87. }
  88. });
  89. }
  90. // 立即初始化链接状态(页面渲染完成后就计算显示)
  91. updateMoveButtons();
  92. // 若模态框打开后表格有异步或动态变更,打开时再次校验状态
  93. $('#col-set').on('shown.bs.modal', function() {
  94. updateMoveButtons();
  95. });
  96. // 模态框隐藏时恢复到初始内容并重新绑定必要状态
  97. $('#col-set').on('hidden.bs.modal', function() {
  98. // 恢复初始 tbody 内容
  99. $tbody.html(_initialColSetTbodyHtml);
  100. // 重新计算按钮显示状态
  101. updateMoveButtons();
  102. });
  103. // 事件委托:从一开始就绑定上/下移事件
  104. $tbody.on('click', '.move-up', function(e) {
  105. e.preventDefault();
  106. const $currentRow = $(this).closest('tr');
  107. const $prevRow = $currentRow.prev('tr');
  108. if ($prevRow.length) {
  109. $currentRow.insertBefore($prevRow);
  110. updateMoveButtons();
  111. }
  112. });
  113. $tbody.on('click', '.move-down', function(e) {
  114. e.preventDefault();
  115. const $currentRow = $(this).closest('tr');
  116. const $nextRow = $currentRow.next('tr');
  117. if ($nextRow.length) {
  118. $currentRow.insertAfter($nextRow);
  119. updateMoveButtons();
  120. }
  121. });
  122. function onSetCol() {
  123. const $tbody = $('#col-set').find('.modal-body table tbody');
  124. const $currentRows = $tbody.find('tr');
  125. const colSet = [];
  126. $currentRows.each(function() {
  127. const $row = $(this);
  128. let alias = '';
  129. const $aliasInput = $row.find('input[type="text"]');
  130. if ($aliasInput.length) {
  131. alias = $aliasInput.val().trim();
  132. }
  133. // 仅组装数据库需要的3个字段
  134. const rowData = {
  135. field: $row.attr('code'), // 字段标识(必填)
  136. show: Number($row.find('.form-check-input').is(':checked')), // 是否显示(必填)
  137. alias: alias // 别名(必填,固定列保存"-")
  138. };
  139. colSet.push(rowData); // 所有行按顺序存入数组,顺序=表格排序
  140. });
  141. $('input[name=col_set]').val(JSON.stringify(colSet));
  142. return true;
  143. }
  144. </script>