tender_showhide.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 localHideList() {
  31. const pro_cate = getLocalCache('pro_'+ pid + '_category_list');
  32. const cate = JSON.stringify(removeValueToCate(category));
  33. if (pro_cate && cate === pro_cate) {
  34. const userTenderHideList = getLocalCache(uphlname);
  35. if (userTenderHideList) {
  36. hideList = JSON.parse(userTenderHideList);
  37. for (const h of hideList) {
  38. const cid = h.sort_id;
  39. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  40. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  41. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  42. doTrStatus(returnItem, 'hide');
  43. }
  44. }
  45. } else {
  46. removeLocalCache(uphlname);
  47. setLocalCache('pro_'+ pid + '_category_list', cate);
  48. }
  49. setTopTr();
  50. }
  51. $(window).resize(setTopTr);
  52. // 设置表头固定并动态调整宽度高度
  53. function setTopTr() {
  54. for(let item = 0; item < $(".c-body table>thead>tr>th").length; item ++) {
  55. $(".c-body table>thead>tr>th").eq(item).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(item).outerWidth());
  56. }
  57. $('.c-body table').css('margin-top', $(".c-body table>thead").height() - 4);
  58. }
  59. function doTrStatus(node, status, all = '') {
  60. if (status === 'show') {
  61. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  62. if (all === 'all') {
  63. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  64. $('.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');
  65. }
  66. } else {
  67. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  68. if (all === 'all') {
  69. if (node.children) {
  70. hideList.push({sort_id: node.sort_id});
  71. setLocalCache(uphlname, JSON.stringify(hideList));
  72. }
  73. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  74. $('.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');
  75. }
  76. }
  77. // 判断是否还有一层
  78. if (node.children && all === '') {
  79. for (const [index,c] of node.children.entries()) {
  80. const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title');
  81. if (title === '收起') {
  82. doTrStatus(c, status);
  83. }
  84. }
  85. } else if (node.children && all === 'all') {
  86. for (const c of node.children) {
  87. doTrStatus(c, status, 'all');
  88. }
  89. }
  90. }
  91. $(document).ready(() => {
  92. // 展开和收起
  93. $('body').on('click', '.fold-switch', function () {
  94. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  95. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  96. $(this).attr('title', '展开');
  97. const cid = $(this).attr('cid');
  98. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  99. doTrStatus(returnItem, 'hide');
  100. hideList.push({sort_id: cid});
  101. setLocalCache(uphlname, JSON.stringify(hideList));
  102. } else {
  103. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  104. $(this).attr('title', '收起');
  105. const cid = $(this).attr('cid');
  106. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  107. doTrStatus(returnItem, 'show');
  108. const index = hideList.findIndex(function(item) {
  109. return parseInt(item.sort_id) === parseInt(cid);
  110. });
  111. hideList.splice(index, 1);
  112. setLocalCache(uphlname, JSON.stringify(hideList));
  113. }
  114. setTopTr();
  115. });
  116. // 一键展开和收起
  117. $('body').on('click', '.tree-toggle', function () {
  118. const item = $(this).attr('data-item');
  119. hideList = [];
  120. if (item === 'open') {
  121. setLocalCache(uphlname, JSON.stringify(hideList));
  122. }
  123. for (const tree of tenderTree) {
  124. if (tree && tree.sort_id !== undefined) {
  125. const cid = tree.sort_id;
  126. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  127. if (item === 'open') {
  128. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  129. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起');
  130. doTrStatus(returnItem, 'show', 'all');
  131. } else if (item === 'hide') {
  132. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  133. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  134. doTrStatus(returnItem, 'hide', 'all');
  135. }
  136. }
  137. }
  138. setTopTr();
  139. })
  140. });