change_industry.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // 模拟数据
  2. var data = {
  3. "data": {
  4. "area": {
  5. "安徽": [],
  6. "福建": ['福州市', '莆田市'],
  7. "广东": [],
  8. },
  9. "buyset": {
  10. "areacount": 1,
  11. "buyerclasscount": 3,
  12. "citys": {
  13. "贵州": 2,
  14. "广东": 2,
  15. "广西": 2,
  16. "福建": 1,
  17. }
  18. },
  19. "industry": [
  20. "安监",
  21. // "保监"
  22. ],
  23. "isTrial": false
  24. },
  25. "errMsg": "",
  26. "success": true
  27. }
  28. var buyCount = data.data.buyset.buyerclasscount;//已购买的行业数量 -1是全部行业,正数为购买的行业数量
  29. var array = data.data.industry;//已选择的行业(数组)
  30. var result = [];//声明一个空数组为选择结果
  31. // 初始化行业数据,渲染到页面
  32. function initData() {
  33. buyCount = data.data.buyset.buyerclasscount;//已购买的行业数量 -1是全部行业,正数为购买的行业数量
  34. array = data.data.industry;//已选择的行业(数组)
  35. /* 已购买过的行业 */
  36. if (buyCount == -1) {
  37. $('.result_name').html('全部行业')
  38. } else {
  39. $('.result_name').html(buyCount + '个行业')
  40. }
  41. /* 已选择的行业 数组为空时即选择的全部行业 不为空时对应选择的结果 */
  42. if (array.length == 0) {
  43. $('.result_count').html('全部行业');
  44. $('.industry_item.whole').addClass('active')
  45. } else {
  46. $('.result_count').html(array.length + '个行业');
  47. $('.industry_item:not(.whole)').each(function () {
  48. var text = $(this).text().trim()
  49. for (var i = 0; i < array.length; i++) {
  50. var element = array[i];
  51. if (element == text) {
  52. $(this).addClass('active')
  53. }
  54. }
  55. })
  56. }
  57. }
  58. // 实时改变选择结果
  59. function resultCount() {
  60. $('.result_count').empty();
  61. var count = $('.list').find('.industry_item.active:not(.whole)').length;
  62. if ($('.industry_item.whole').hasClass('active')) {
  63. $('.result_count').html('全部行业');
  64. result = [];
  65. if (buyCount != -1) {
  66. $('.update_tips').show();
  67. } else {
  68. $('.update_tips').hide();
  69. }
  70. } else {
  71. var buttons = $('.select-area-box .list button:not(.whole)');
  72. var canChangeArr = [];
  73. buttons.each(function () {
  74. if ($(this).hasClass('active')) {
  75. canChangeArr.push($(this).text())
  76. }
  77. })
  78. if (count > buyCount) {
  79. $('.update_tips').show();
  80. } else {
  81. $('.update_tips').hide();
  82. }
  83. $('.result_count').html(count + '个行业');
  84. if ($('.list').find('.industry_item.active:not(.whole)').length === 0) {
  85. $('.save-btn').prop('disabled', true)
  86. } else {
  87. $('.save-btn').prop('disabled', false)
  88. }
  89. result = canChangeArr;
  90. }
  91. }
  92. $(function () {
  93. // 公共资源交易介绍提示框
  94. $('#showTips').click(function (e) {
  95. e.stopPropagation();
  96. $('.tips_detail').slideToggle()
  97. });
  98. // 点击导航跳转
  99. $("body").on('click', '.slide a', function () {
  100. var s = $(this).html()
  101. if (s == '#') {
  102. return;
  103. }
  104. document.querySelector('#' + s).scrollIntoView({
  105. block: 'center'
  106. });
  107. })
  108. // 选择全部行业
  109. $('.industry_item.whole').on('click', function () {
  110. $(this).addClass('active').parents('.list').find('.industry_item:not(.whole)').removeClass('active');
  111. resultCount();
  112. })
  113. // 选择非全部行业
  114. $('.industry_item:not(.whole)').on('click', function () {
  115. $('.industry_item.whole').removeClass('active');
  116. $(this).toggleClass('active');
  117. resultCount();
  118. })
  119. // 重置按钮事件
  120. $('.reset-btn').on('click', function () {
  121. $('.list').find('.industry_item').removeClass('active');
  122. $('.update_tips').hide();
  123. initData();
  124. })
  125. // 确认按钮事件
  126. $('.save-btn').on('click', function () {
  127. data.data.industry = result
  128. sessionStorage.setItem('sub_vip_state', JSON.stringify(data.data))
  129. history.back()
  130. })
  131. function getDataWitXHR() {
  132. $DoPost('/subscribepay/vipsubscribe/getSubBuyMsg', {}, function (res) {
  133. if (!res.success) return
  134. data.data = res.data
  135. sessionStorage.setItem('sub_vip_state', JSON.stringify(res.data))
  136. initData();
  137. })
  138. }
  139. var subVipState = sessionStorage.getItem('sub_vip_state')
  140. if (subVipState) {
  141. data.data = JSON.parse(subVipState)
  142. initData();
  143. } else {
  144. getDataWitXHR()
  145. }
  146. })