tender_showhide.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. for(let item = 0; item < $(".c-body table>thead>tr>th").length; item ++) {
  50. $(".c-body table>thead>tr>th").eq(item).outerWidth($(".c-body table>tbody>tr:last").children('td').eq(item).outerWidth());
  51. }
  52. $('.c-body table').css('margin-top', $(".c-body table>thead").height() - 4);
  53. }
  54. function doTrStatus(node, status, all = '') {
  55. if (status === 'show') {
  56. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  57. if (all === 'all') {
  58. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  59. $('.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');
  60. }
  61. } else {
  62. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  63. if (all === 'all') {
  64. if (node.children) {
  65. hideList.push({sort_id: node.sort_id});
  66. setLocalCache(uphlname, JSON.stringify(hideList));
  67. }
  68. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  69. $('.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');
  70. }
  71. }
  72. // 判断是否还有一层
  73. if (node.children && all === '') {
  74. for (const [index,c] of node.children.entries()) {
  75. const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title');
  76. if (title === '收起') {
  77. doTrStatus(c, status);
  78. }
  79. }
  80. } else if (node.children && all === 'all') {
  81. for (const c of node.children) {
  82. doTrStatus(c, status, 'all');
  83. }
  84. }
  85. }
  86. $(document).ready(() => {
  87. // 展开和收起
  88. $('body').on('click', '.fold-switch', function () {
  89. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  90. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  91. $(this).attr('title', '展开');
  92. const cid = $(this).attr('cid');
  93. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  94. doTrStatus(returnItem, 'hide');
  95. hideList.push({sort_id: cid});
  96. setLocalCache(uphlname, JSON.stringify(hideList));
  97. } else {
  98. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  99. $(this).attr('title', '收起');
  100. const cid = $(this).attr('cid');
  101. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  102. doTrStatus(returnItem, 'show');
  103. const index = hideList.findIndex(function(item) {
  104. return parseInt(item.sort_id) === parseInt(cid);
  105. });
  106. hideList.splice(index, 1);
  107. setLocalCache(uphlname, JSON.stringify(hideList));
  108. }
  109. });
  110. // 一键展开和收起
  111. $('body').on('click', '.tree-toggle', function () {
  112. const item = $(this).attr('data-item');
  113. hideList = [];
  114. if (item === 'open') {
  115. setLocalCache(uphlname, JSON.stringify(hideList));
  116. }
  117. for (const tree of tenderTree) {
  118. if (tree && tree.sort_id !== undefined) {
  119. const cid = tree.sort_id;
  120. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  121. if (item === 'open') {
  122. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  123. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起');
  124. doTrStatus(returnItem, 'show', 'all');
  125. } else if (item === 'hide') {
  126. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  127. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  128. doTrStatus(returnItem, 'hide', 'all');
  129. }
  130. }
  131. }
  132. })
  133. });