tender_showhide.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. const colSpanNum = $(".c-body table>thead>tr").eq(1).children("th").length;
  113. if (colSpanNum === 0) {
  114. for(let item = 0; item < $(".c-body table>thead>tr>th").length; item ++) {
  115. $(".c-body table>thead>tr>th").eq(item).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(item).outerWidth());
  116. }
  117. } else {
  118. const colSpanArr = [];
  119. let colSpanIndex = 0;
  120. for (let item = 0; item < $(".c-body table>thead>tr").eq(0).children("th").length; item ++) {
  121. if ($(".c-body table>thead>tr").eq(0).children('th').eq(item).attr('colspan') === undefined) {
  122. $(".c-body table>thead>tr").eq(0).children('th').eq(item).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(colSpanIndex).outerWidth());
  123. colSpanIndex = colSpanIndex + 1;
  124. } else {
  125. colSpanIndex = colSpanIndex + parseInt($(".c-body table>thead>tr").eq(0).children('th').eq(item).attr('colspan'));
  126. colSpanArr.push({
  127. num: parseInt($(".c-body table>thead>tr").eq(0).children('th').eq(item).attr('colspan')),
  128. end: colSpanIndex,
  129. });
  130. }
  131. }
  132. let colStart = 0;
  133. for (const col of colSpanArr) {
  134. for (let item = col.num; item > 0; item--) {
  135. $(".c-body table>thead>tr").eq(1).children('th').eq(colStart).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(col.end - item).outerWidth());
  136. colStart = colStart + 1;
  137. }
  138. }
  139. }
  140. $('.c-body table').css('margin-top', $(".c-body table>thead").height() - 4);
  141. }
  142. function doTrStatus(node, status, all = '') {
  143. if (node) {
  144. if (status === 'show') {
  145. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  146. if (all === 'all') {
  147. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  148. $('.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');
  149. }
  150. } else {
  151. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  152. if (all === 'all') {
  153. if (node.children) {
  154. hideList.push({sort_id: node.sort_id});
  155. setLocalCache(uphlname, JSON.stringify(hideList));
  156. }
  157. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  158. $('.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');
  159. }
  160. }
  161. // 判断是否还有一层
  162. if (node.children && all === '') {
  163. for (const [index,c] of node.children.entries()) {
  164. const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title');
  165. if (title === '收起') {
  166. doTrStatus(c, status);
  167. }
  168. }
  169. } else if (node.children && all === 'all') {
  170. for (const c of node.children) {
  171. doTrStatus(c, status, 'all');
  172. }
  173. }
  174. }
  175. }
  176. let tenderTreeShowLevel;
  177. const getChildrenLevel = function (node) {
  178. let iLevel = node.level || 1;
  179. if (node.children && node.children.length > 0) {
  180. for (const c of node.children) {
  181. iLevel = Math.max(iLevel, getChildrenLevel(c));
  182. }
  183. }
  184. return iLevel;
  185. };
  186. $(document).ready(() => {
  187. // 展开和收起
  188. $('body').on('click', '.fold-switch', function () {
  189. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  190. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  191. $(this).attr('title', '展开');
  192. const cid = $(this).attr('cid');
  193. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  194. doTrStatus(returnItem, 'hide');
  195. hideList.push({sort_id: cid});
  196. setLocalCache(uphlname, JSON.stringify(hideList));
  197. } else {
  198. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  199. $(this).attr('title', '收起');
  200. const cid = $(this).attr('cid');
  201. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  202. doTrStatus(returnItem, 'show');
  203. const index = hideList.findIndex(function(item) {
  204. return parseInt(item.sort_id) === parseInt(cid);
  205. });
  206. hideList.splice(index, 1);
  207. setLocalCache(uphlname, JSON.stringify(hideList));
  208. }
  209. setTopTr();
  210. });
  211. // 一键展开和收起
  212. $('body').on('click', '.tree-toggle', function () {
  213. const item = $(this).attr('data-item');
  214. hideList = [];
  215. if (item === 'open') {
  216. setLocalCache(uphlname, JSON.stringify(hideList));
  217. }
  218. for (const tree of tenderTree) {
  219. if (tree && tree.sort_id !== undefined) {
  220. const cid = tree.sort_id;
  221. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  222. if (item === 'open') {
  223. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  224. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起');
  225. doTrStatus(returnItem, 'show', 'all');
  226. } else if (item === 'hide') {
  227. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  228. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  229. doTrStatus(returnItem, 'hide', 'all');
  230. }
  231. }
  232. }
  233. setTopTr();
  234. });
  235. tenderTreeShowLevel = $.cs_showLevel({
  236. selector: '#show-level',
  237. levels: [
  238. {
  239. type: 'sort', count: 5, visible_count: function () {
  240. return tenderTree.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1;
  241. }
  242. },
  243. {
  244. type: 'last', title: '最底层', visible: function () {
  245. const count = tenderTree.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1;
  246. return count > 0;
  247. }
  248. },
  249. ],
  250. showLevel: function (tag) {
  251. switch (tag) {
  252. case "1":
  253. case "2":
  254. case "3":
  255. case "4":
  256. case "5":
  257. expandByLevel(parseInt(tag));
  258. break;
  259. case "last":
  260. expandByLevel(20);
  261. break;
  262. default: return;
  263. }
  264. }
  265. });
  266. });