tender_showhide.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. }
  47. }
  48. for (const tender of teTree) {
  49. if (tender.children) {
  50. sortTenderTree(tender.children);
  51. }
  52. }
  53. }
  54. function localHideList(wap = false) {
  55. const pro_cate = getLocalCache('pro_'+ pid + '_category_list');
  56. const cate = JSON.stringify(removeValueToCate(category));
  57. if (pro_cate && cate === pro_cate) {
  58. const userTenderHideList = getLocalCache(uphlname);
  59. if (userTenderHideList) {
  60. hideList = JSON.parse(userTenderHideList);
  61. for (const h of hideList) {
  62. const cid = h.sort_id;
  63. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  64. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  65. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  66. doTrStatus(returnItem, 'hide');
  67. }
  68. }
  69. } else {
  70. removeLocalCache(uphlname);
  71. setLocalCache('pro_'+ pid + '_category_list', cate);
  72. }
  73. if (!wap)
  74. setTopTr();
  75. }
  76. $(window).resize(setTopTr);
  77. // 设置表头固定并动态调整宽度高度
  78. function setTopTr() {
  79. for(let item = 0; item < $(".c-body table>thead>tr>th").length; item ++) {
  80. $(".c-body table>thead>tr>th").eq(item).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(item).outerWidth());
  81. }
  82. $('.c-body table').css('margin-top', $(".c-body table>thead").height() - 4);
  83. // $('.c-body table').css('margin-top', -2);
  84. }
  85. function doTrStatus(node, status, all = '') {
  86. if (node) {
  87. if (status === 'show') {
  88. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  89. if (all === 'all') {
  90. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  91. $('.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');
  92. }
  93. } else {
  94. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  95. if (all === 'all') {
  96. if (node.children) {
  97. hideList.push({sort_id: node.sort_id});
  98. setLocalCache(uphlname, JSON.stringify(hideList));
  99. }
  100. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  101. $('.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');
  102. }
  103. }
  104. // 判断是否还有一层
  105. if (node.children && all === '') {
  106. for (const [index,c] of node.children.entries()) {
  107. const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title');
  108. if (title === '收起') {
  109. doTrStatus(c, status);
  110. }
  111. }
  112. } else if (node.children && all === 'all') {
  113. for (const c of node.children) {
  114. doTrStatus(c, status, 'all');
  115. }
  116. }
  117. }
  118. }
  119. $(document).ready(() => {
  120. // 展开和收起
  121. $('body').on('click', '.fold-switch', function () {
  122. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  123. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  124. $(this).attr('title', '展开');
  125. const cid = $(this).attr('cid');
  126. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  127. doTrStatus(returnItem, 'hide');
  128. hideList.push({sort_id: cid});
  129. setLocalCache(uphlname, JSON.stringify(hideList));
  130. } else {
  131. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  132. $(this).attr('title', '收起');
  133. const cid = $(this).attr('cid');
  134. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  135. doTrStatus(returnItem, 'show');
  136. const index = hideList.findIndex(function(item) {
  137. return parseInt(item.sort_id) === parseInt(cid);
  138. });
  139. hideList.splice(index, 1);
  140. setLocalCache(uphlname, JSON.stringify(hideList));
  141. }
  142. setTopTr();
  143. });
  144. // 一键展开和收起
  145. $('body').on('click', '.tree-toggle', function () {
  146. const item = $(this).attr('data-item');
  147. hideList = [];
  148. if (item === 'open') {
  149. setLocalCache(uphlname, JSON.stringify(hideList));
  150. }
  151. for (const tree of tenderTree) {
  152. if (tree && tree.sort_id !== undefined) {
  153. const cid = tree.sort_id;
  154. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  155. if (item === 'open') {
  156. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  157. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起');
  158. doTrStatus(returnItem, 'show', 'all');
  159. } else if (item === 'hide') {
  160. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  161. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  162. doTrStatus(returnItem, 'hide', 'all');
  163. }
  164. }
  165. }
  166. setTopTr();
  167. })
  168. });