tender_showhide.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author EllisRan
  6. * @date 2020/03/06
  7. * @version
  8. */
  9. let hideList = [];
  10. let returnItem;
  11. const findTenderTreeNode = function(sortId, tree) {
  12. tree.forEach((item) => {
  13. if (item.sort_id !== undefined && item.sort_id === sortId) {
  14. returnItem = item;
  15. return item;
  16. } else if (item.children && item.children.length > 0) {
  17. findTenderTreeNode(sortId, item.children);
  18. }
  19. });
  20. }
  21. function removeValueToCate(cate) {
  22. const changeCate = JSON.parse(JSON.stringify(cate));
  23. const newCate = [];
  24. for (const c of changeCate) {
  25. delete c.value;
  26. newCate.push(c);
  27. }
  28. return newCate;
  29. }
  30. // 根据标段类别设置排序
  31. function sortTenderTree(teTree = tenderTree) {
  32. for (const tender of teTree) {
  33. if (tender.sort) {
  34. // 当前层排序
  35. teTree.sort(function (a, b) {
  36. if (a.sort && b.sort) {
  37. return a.sort - b.sort;
  38. } else if (a.sort && !b.sort) {
  39. return -1;
  40. } else if (b.sort && !a.sort) {
  41. return 1;
  42. } else {
  43. return 0;
  44. }
  45. });
  46. sortTenderTree(tender.children);
  47. }
  48. }
  49. }
  50. function localHideList(wap = false) {
  51. const pro_cate = getLocalCache('pro_'+ pid + '_category_list');
  52. const cate = JSON.stringify(removeValueToCate(category));
  53. if (pro_cate && cate === pro_cate) {
  54. const userTenderHideList = getLocalCache(uphlname);
  55. if (userTenderHideList) {
  56. hideList = JSON.parse(userTenderHideList);
  57. for (const h of hideList) {
  58. const cid = h.sort_id;
  59. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  60. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  61. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  62. doTrStatus(returnItem, 'hide');
  63. }
  64. }
  65. } else {
  66. removeLocalCache(uphlname);
  67. setLocalCache('pro_'+ pid + '_category_list', cate);
  68. }
  69. if (!wap)
  70. setTopTr();
  71. }
  72. $(window).resize(setTopTr);
  73. // 设置表头固定并动态调整宽度高度
  74. function setTopTr() {
  75. for(let item = 0; item < $(".c-body table>thead>tr>th").length; item ++) {
  76. $(".c-body table>thead>tr>th").eq(item).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(item).outerWidth());
  77. }
  78. $('.c-body table').css('margin-top', $(".c-body table>thead").height() - 4);
  79. // $('.c-body table').css('margin-top', -2);
  80. }
  81. function doTrStatus(node, status, all = '') {
  82. if (node) {
  83. if (status === 'show') {
  84. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  85. if (all === 'all') {
  86. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  87. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch i').removeClass('fa-plus-square-o').removeClass('fa-minus-square-o').addClass('fa-minus-square-o');
  88. }
  89. } else {
  90. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  91. if (all === 'all') {
  92. if (node.children) {
  93. hideList.push({sort_id: node.sort_id});
  94. setLocalCache(uphlname, JSON.stringify(hideList));
  95. }
  96. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  97. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch i').removeClass('fa-minus-square-o').removeClass('fa-plus-square-o').addClass('fa-plus-square-o');
  98. }
  99. }
  100. // 判断是否还有一层
  101. if (node.children && all === '') {
  102. for (const [index,c] of node.children.entries()) {
  103. const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title');
  104. if (title === '收起') {
  105. doTrStatus(c, status);
  106. }
  107. }
  108. } else if (node.children && all === 'all') {
  109. for (const c of node.children) {
  110. doTrStatus(c, status, 'all');
  111. }
  112. }
  113. }
  114. }
  115. $(document).ready(() => {
  116. // 展开和收起
  117. $('body').on('click', '.fold-switch', function () {
  118. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  119. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  120. $(this).attr('title', '展开');
  121. const cid = $(this).attr('cid');
  122. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  123. doTrStatus(returnItem, 'hide');
  124. hideList.push({sort_id: cid});
  125. setLocalCache(uphlname, JSON.stringify(hideList));
  126. } else {
  127. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  128. $(this).attr('title', '收起');
  129. const cid = $(this).attr('cid');
  130. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  131. doTrStatus(returnItem, 'show');
  132. const index = hideList.findIndex(function(item) {
  133. return parseInt(item.sort_id) === parseInt(cid);
  134. });
  135. hideList.splice(index, 1);
  136. setLocalCache(uphlname, JSON.stringify(hideList));
  137. }
  138. setTopTr();
  139. });
  140. // 一键展开和收起
  141. $('body').on('click', '.tree-toggle', function () {
  142. const item = $(this).attr('data-item');
  143. hideList = [];
  144. if (item === 'open') {
  145. setLocalCache(uphlname, JSON.stringify(hideList));
  146. }
  147. for (const tree of tenderTree) {
  148. if (tree && tree.sort_id !== undefined) {
  149. const cid = tree.sort_id;
  150. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  151. if (item === 'open') {
  152. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  153. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起');
  154. doTrStatus(returnItem, 'show', 'all');
  155. } else if (item === 'hide') {
  156. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  157. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  158. doTrStatus(returnItem, 'hide', 'all');
  159. }
  160. }
  161. }
  162. setTopTr();
  163. })
  164. });