tender_showhide.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. let hideList = [];
  3. let returnItem;
  4. const findTenderTreeNode = function(sortId, tree) {
  5. tree.forEach((item) => {
  6. if (item.sort_id !== undefined && item.sort_id === sortId) {
  7. returnItem = item;
  8. return item;
  9. } else if (item.children && item.children.length > 0) {
  10. findTenderTreeNode(sortId, item.children);
  11. }
  12. });
  13. }
  14. function removeValueToCate(cate) {
  15. const changeCate = JSON.parse(JSON.stringify(cate));
  16. const newCate = [];
  17. for (const c of changeCate) {
  18. delete c.value;
  19. newCate.push(c);
  20. }
  21. return newCate;
  22. }
  23. function localHideList() {
  24. const pro_cate = getLocalCache('pro_'+ pid + '_category_list');
  25. const cate = JSON.stringify(removeValueToCate(category));
  26. if (pro_cate && cate === pro_cate) {
  27. const userTenderHideList = getLocalCache(uphlname);
  28. if (userTenderHideList) {
  29. hideList = JSON.parse(userTenderHideList);
  30. for (const h of hideList) {
  31. const cid = h.sort_id;
  32. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  33. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  34. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  35. doTrStatus(returnItem, 'hide');
  36. }
  37. }
  38. } else {
  39. removeLocalCache(uphlname);
  40. setLocalCache('pro_'+ pid + '_category_list', cate);
  41. }
  42. }
  43. function doTrStatus(node, status, all = '') {
  44. if (status === 'show') {
  45. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  46. if (all === 'all') {
  47. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  48. $('.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');
  49. }
  50. } else {
  51. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  52. if (all === 'all') {
  53. if (node.children) {
  54. hideList.push({sort_id: node.sort_id});
  55. setLocalCache(uphlname, JSON.stringify(hideList));
  56. }
  57. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  58. $('.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');
  59. }
  60. }
  61. // 判断是否还有一层
  62. if (node.children && all === '') {
  63. for (const [index,c] of node.children.entries()) {
  64. const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title');
  65. if (title === '收起') {
  66. doTrStatus(c, status);
  67. }
  68. }
  69. } else if (node.children && all === 'all') {
  70. for (const c of node.children) {
  71. doTrStatus(c, status, 'all');
  72. }
  73. }
  74. }
  75. $(document).ready(() => {
  76. // 展开和收起
  77. $('body').on('click', '.fold-switch', function () {
  78. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  79. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  80. $(this).attr('title', '展开');
  81. const cid = $(this).attr('cid');
  82. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  83. doTrStatus(returnItem, 'hide');
  84. hideList.push({sort_id: cid});
  85. setLocalCache(uphlname, JSON.stringify(hideList));
  86. } else {
  87. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  88. $(this).attr('title', '收起');
  89. const cid = $(this).attr('cid');
  90. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  91. doTrStatus(returnItem, 'show');
  92. const index = hideList.findIndex(function(item) {
  93. return parseInt(item.sort_id) === parseInt(cid);
  94. });
  95. hideList.splice(index, 1);
  96. setLocalCache(uphlname, JSON.stringify(hideList));
  97. }
  98. });
  99. // 一键展开和收起
  100. $('body').on('click', '.tree-toggle', function () {
  101. const item = $(this).attr('data-item');
  102. hideList = [];
  103. if (item === 'open') {
  104. setLocalCache(uphlname, JSON.stringify(hideList));
  105. }
  106. for (const tree of tenderTree) {
  107. if (tree && tree.sort_id !== undefined) {
  108. const cid = tree.sort_id;
  109. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  110. if (item === 'open') {
  111. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  112. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起');
  113. doTrStatus(returnItem, 'show', 'all');
  114. } else if (item === 'hide') {
  115. $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  116. $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开');
  117. doTrStatus(returnItem, 'hide', 'all');
  118. }
  119. }
  120. }
  121. })
  122. });