tender_showhide.js 9.5 KB

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