tender_showhide.js 9.5 KB

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