tender_showhide.js 9.4 KB

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