tender_showhide.js 5.4 KB

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