tender_showhide.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. function recursiveExpand(nodes, parent, checkFun) {
  31. for (const node of nodes) {
  32. const expanded = checkFun(node);
  33. if (!expanded && node.sort_id) hideList.push({sort_id: node.sort_id});
  34. if (node.expanded !== expanded) {
  35. node.expanded = expanded;
  36. if (node.sort_id) {
  37. if (node.expanded) {
  38. $('.c-body tr td span[cid="' + node.sort_id + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  39. $('.c-body tr td span[cid="' + node.sort_id + '"]').attr('title', '收起');
  40. } else {
  41. $('.c-body tr td span[cid="' + node.sort_id + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  42. $('.c-body tr td span[cid="' + node.sort_id + '"]').attr('title', '展开');
  43. }
  44. doTrStatus(node, node.expanded ? 'show' : 'hide');
  45. }
  46. }
  47. node.visible = parent ? (parent.expanded && parent.visible) : true;
  48. if (node.children) recursiveExpand(node.children, node, checkFun);
  49. }
  50. }
  51. function expandByCustom(checkFun) {
  52. hideList = [];
  53. recursiveExpand(tenderTree, null, checkFun);
  54. }
  55. function expandByLevel(level) {
  56. expandByCustom(function (n) {
  57. return n.level < level;
  58. });
  59. setLocalCache(uphlname, JSON.stringify(hideList));
  60. }
  61. // 根据标段类别设置排序
  62. function sortTenderTree(teTree = tenderTree) {
  63. for (const tender of teTree) {
  64. if (tender.sort) {
  65. // 当前层排序
  66. teTree.sort(function (a, b) {
  67. if (a.sort && b.sort) {
  68. return a.sort - b.sort;
  69. } else if (a.sort && !b.sort) {
  70. return -1;
  71. } else if (b.sort && !a.sort) {
  72. return 1;
  73. } else {
  74. return 0;
  75. }
  76. });
  77. }
  78. }
  79. for (const tender of teTree) {
  80. if (tender.children) {
  81. sortTenderTree(tender.children);
  82. }
  83. }
  84. }
  85. function localHideList(wap = false) {
  86. const pro_cate = getLocalCache('pro_'+ pid + '_category_list');
  87. const cate = JSON.stringify(removeValueToCate(category));
  88. if (pro_cate && cate === pro_cate) {
  89. const userTenderHideList = getLocalCache(uphlname);
  90. if (userTenderHideList) {
  91. hideList = JSON.parse(userTenderHideList);
  92. for (const h of hideList) {
  93. const cid = h.sort_id;
  94. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  95. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  96. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  97. doTrStatus(returnItem, 'hide');
  98. }
  99. }
  100. } else {
  101. removeLocalCache(uphlname);
  102. setLocalCache('pro_'+ pid + '_category_list', cate);
  103. }
  104. if (!wap)
  105. setTopTr();
  106. }
  107. $(window).resize(setTopTr);
  108. // 设置表头固定并动态调整宽度高度
  109. function setTopTr() {
  110. for(let item = 0; item < $(".c-body table>thead>tr>th").length; item ++) {
  111. $(".c-body table>thead>tr>th").eq(item).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(item).outerWidth());
  112. }
  113. $('.c-body table').css('margin-top', $(".c-body table>thead").height() - 4);
  114. // $('.c-body table').css('margin-top', -2);
  115. }
  116. function doTrStatus(node, status, all = '') {
  117. if (node) {
  118. if (status === 'show') {
  119. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  120. if (all === 'all') {
  121. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  122. $('.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');
  123. }
  124. } else {
  125. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  126. if (all === 'all') {
  127. if (node.children) {
  128. hideList.push({sort_id: node.sort_id});
  129. setLocalCache(uphlname, JSON.stringify(hideList));
  130. }
  131. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  132. $('.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');
  133. }
  134. }
  135. // 判断是否还有一层
  136. if (node.children && all === '') {
  137. for (const [index,c] of node.children.entries()) {
  138. const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title');
  139. if (title === '收起') {
  140. doTrStatus(c, status);
  141. }
  142. }
  143. } else if (node.children && all === 'all') {
  144. for (const c of node.children) {
  145. doTrStatus(c, status, 'all');
  146. }
  147. }
  148. }
  149. }
  150. let tenderTreeShowLevel;
  151. const getChildrenLevel = function (node) {
  152. let iLevel = node.level || 1;
  153. if (node.children && node.children.length > 0) {
  154. for (const c of node.children) {
  155. iLevel = Math.max(iLevel, getChildrenLevel(c));
  156. }
  157. }
  158. return iLevel;
  159. };
  160. $(document).ready(() => {
  161. // 展开和收起
  162. $('body').on('click', '.fold-switch', function () {
  163. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  164. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  165. $(this).attr('title', '展开');
  166. const cid = $(this).attr('cid');
  167. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  168. doTrStatus(returnItem, 'hide');
  169. hideList.push({sort_id: cid});
  170. setLocalCache(uphlname, JSON.stringify(hideList));
  171. } else {
  172. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  173. $(this).attr('title', '收起');
  174. const cid = $(this).attr('cid');
  175. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  176. doTrStatus(returnItem, 'show');
  177. const index = hideList.findIndex(function(item) {
  178. return parseInt(item.sort_id) === parseInt(cid);
  179. });
  180. hideList.splice(index, 1);
  181. setLocalCache(uphlname, JSON.stringify(hideList));
  182. }
  183. setTopTr();
  184. });
  185. // 一键展开和收起
  186. $('body').on('click', '.tree-toggle', function () {
  187. const item = $(this).attr('data-item');
  188. hideList = [];
  189. if (item === 'open') {
  190. setLocalCache(uphlname, JSON.stringify(hideList));
  191. }
  192. for (const tree of tenderTree) {
  193. if (tree && tree.sort_id !== undefined) {
  194. const cid = tree.sort_id;
  195. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  196. if (item === 'open') {
  197. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  198. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起');
  199. doTrStatus(returnItem, 'show', 'all');
  200. } else if (item === 'hide') {
  201. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  202. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  203. doTrStatus(returnItem, 'hide', 'all');
  204. }
  205. }
  206. }
  207. setTopTr();
  208. });
  209. tenderTreeShowLevel = $.cs_showLevel({
  210. selector: '#show-level',
  211. levels: [
  212. {
  213. type: 'sort', count: 5, visible_count: function () {
  214. return tenderTree.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1;
  215. }
  216. },
  217. {
  218. type: 'last', title: '最底层', visible: function () {
  219. const count = tenderTree.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1;
  220. return count > 0;
  221. }
  222. },
  223. ],
  224. showLevel: function (tag) {
  225. switch (tag) {
  226. case "1":
  227. case "2":
  228. case "3":
  229. case "4":
  230. case "5":
  231. expandByLevel(parseInt(tag));
  232. break;
  233. case "last":
  234. expandByLevel(20);
  235. break;
  236. default: return;
  237. }
  238. }
  239. });
  240. });