col_set.ejs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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">列名</th><th width="50px">显示</th><th width="150px">别名</th><th width="80px">操作</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. <a href="javascript:;" class="move-up text-primary mr-2" style="text-decoration: none;">上移</a>
  42. <a href="javascript:;" class="move-down text-primary" style="text-decoration: none;">下移</a>
  43. </td>
  44. </tr>
  45. <% } %>
  46. </tbody>
  47. </table>
  48. </div>
  49. <form class="modal-footer" action="/sp/<%- ctx.subProject.id %>/contract/<%- ctx.contractOptions.tid ? 'tender/'+ctx.contractOptions.tid + '/' : '' %>col-set" method="post" onsubmit="return onSetCol();">
  50. <input type="hidden" name="_csrf_j" value="<%= ctx.csrf %>">
  51. <input type="hidden" name="col_set" value="">
  52. <input type="hidden" name="type" value="<%- ctx.contract_type %>">
  53. <input type="hidden" name="col_type" value="info">
  54. <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">关闭</button>
  55. <button type="submit" class="btn btn-primary btn-sm" id="add-bd-ok">确定修改</button>
  56. </form>
  57. </div>
  58. </div>
  59. </div>
  60. <script>
  61. // 立即获取表格 tbody 并绑定事件(确保脚本加载时即可初始化)
  62. const $tbody = $('#col-set').find('.modal-body table tbody');
  63. // 更新上下移链接显示状态
  64. function updateMoveButtons() {
  65. const $currentRows = $tbody.find('tr');
  66. const totalRows = $currentRows.length;
  67. $currentRows.each(function(index) {
  68. const $row = $(this);
  69. const $upLink = $row.find('.move-up');
  70. const $downLink = $row.find('.move-down');
  71. if (totalRows === 1) {
  72. $upLink.hide();
  73. $downLink.hide();
  74. } else if (index === 0) {
  75. $upLink.hide();
  76. $downLink.show();
  77. } else if (index === totalRows - 1) {
  78. $upLink.show();
  79. $downLink.hide();
  80. } else {
  81. $upLink.show();
  82. $downLink.show();
  83. }
  84. });
  85. }
  86. // 立即初始化链接状态(页面渲染完成后就计算显示)
  87. updateMoveButtons();
  88. // 若模态框打开后表格有异步或动态变更,打开时再次校验状态
  89. $('#col-set').on('shown.bs.modal', function() {
  90. updateMoveButtons();
  91. });
  92. // 事件委托:从一开始就绑定上/下移事件
  93. $tbody.on('click', '.move-up', function(e) {
  94. e.preventDefault();
  95. const $currentRow = $(this).closest('tr');
  96. const $prevRow = $currentRow.prev('tr');
  97. if ($prevRow.length) {
  98. $currentRow.insertBefore($prevRow);
  99. updateMoveButtons();
  100. }
  101. });
  102. $tbody.on('click', '.move-down', function(e) {
  103. e.preventDefault();
  104. const $currentRow = $(this).closest('tr');
  105. const $nextRow = $currentRow.next('tr');
  106. if ($nextRow.length) {
  107. $currentRow.insertAfter($nextRow);
  108. updateMoveButtons();
  109. }
  110. });
  111. function onSetCol() {
  112. const $tbody = $('#col-set').find('.modal-body table tbody');
  113. const $currentRows = $tbody.find('tr');
  114. const colSet = [];
  115. $currentRows.each(function() {
  116. const $row = $(this);
  117. let alias = '';
  118. const $aliasInput = $row.find('input[type="text"]');
  119. if ($aliasInput.length) {
  120. alias = $aliasInput.val().trim();
  121. }
  122. // 仅组装数据库需要的3个字段
  123. const rowData = {
  124. field: $row.attr('code'), // 字段标识(必填)
  125. show: Number($row.find('.form-check-input').is(':checked')), // 是否显示(必填)
  126. alias: alias // 别名(必填,固定列保存"-")
  127. };
  128. colSet.push(rowData); // 所有行按顺序存入数组,顺序=表格排序
  129. });
  130. $('input[name=col_set]').val(JSON.stringify(colSet));
  131. return true;
  132. }
  133. </script>