index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view>
  3. <view class="tabs-container">
  4. <view class="tabs-row no-gap">
  5. <view v-for="(item, index) in list" :key="item.id"
  6. :class="tabKey === index ? 'tab-item tab-active' : 'tab-item'" @click="handlTabs(item, index)">
  7. {{ item.businessName }}
  8. </view>
  9. </view>
  10. <view class="tabs-row no-gap">
  11. <view v-for="(item, index) in childrenList" :key="item.id"
  12. :class="childrenKey === index ? 'tab-item tab-active' : 'tab-item'"
  13. @click="childernChange(item, index)">
  14. {{ item.businessName }}
  15. </view>
  16. </view>
  17. <view class="tabs-row no-gap">
  18. <view v-for="(item, index) in children2List" :key="item.id"
  19. :class="children2Key === index ? 'tab-item tab-active' : 'tab-item'"
  20. @click="childern2Change(item, index)">
  21. {{ item.businessName }}
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script setup>
  28. import {
  29. ref,
  30. onMounted,
  31. reactive,
  32. nextTick,
  33. computed
  34. } from 'vue';
  35. import {
  36. onLoad,
  37. onShow,
  38. onReachBottom
  39. } from "@dcloudio/uni-app"
  40. const emits = defineEmits(['change'])
  41. const tabKey = ref(0);//tab的下标
  42. const secondRowTabKey = ref(0);//第二行tab的下标
  43. const childrenKey = ref(null);//二级的id
  44. const children2Key = ref(null);//三级的id
  45. const list = ref([])
  46. const splitIndex = computed(() => Math.ceil(list.value.length / 2)); // 计算分割点
  47. // 获取第一行的tabs
  48. const getFirstRowTabs = () => {
  49. return list.value.slice(0, splitIndex.value);
  50. }
  51. // 获取第二行的tabs
  52. const getSecondRowTabs = () => {
  53. return list.value.slice(splitIndex.value);
  54. }
  55. const childrenList = ref([]);//二级的数据
  56. const children2List = ref([]);//三级的数据
  57. // 处理第一行标签点击
  58. const handlTabs = (e, index) => {
  59. childrenList.value = e.children ? [{
  60. businessName: '全部',
  61. id: e.id
  62. }, ...e.children] : []
  63. tabKey.value = index;//记录下标
  64. secondRowTabKey.value = -1; // 重置第二行选中状态
  65. childrenKey.value = 0;
  66. children2Key.value = 0;
  67. children2List.value = [];
  68. emits('change', e)
  69. }
  70. // 处理第二行标签点击
  71. const handlSecondRowTabs = (e, index) => {
  72. childrenList.value = e.children ? [{
  73. businessName: '全部',
  74. id: e.id
  75. }, ...e.children] : []
  76. secondRowTabKey.value = index;//记录下标
  77. tabKey.value = -1; // 重置第一行选中状态
  78. childrenKey.value = 0;
  79. children2Key.value = 0;
  80. children2List.value = [];
  81. emits('change', e)
  82. }
  83. const childernChange = (e, index) => {
  84. children2List.value = e.children ? [{
  85. businessName: '全部',
  86. id: e.id
  87. }, ...e.children] : [];
  88. children2Key.value = 0;
  89. childrenKey.value = index;
  90. emits('change', e)
  91. }
  92. const childern2Change = (e, index) => {
  93. children2Key.value = index;
  94. emits('change', e)
  95. }
  96. onMounted(() => {
  97. })
  98. onLoad((options) => {
  99. const dataList = JSON.parse(decodeURIComponent(options.dataList));
  100. list.value = dataList
  101. if (options.id) {
  102. const tab = dataList.find(item => item.id === options.id);
  103. const index = dataList.findIndex(item => item.id === options.id);
  104. // 判断点击的是第一行还是第二行
  105. if (index < splitIndex.value) {
  106. handlTabs(tab, index);
  107. } else {
  108. // 计算在第二行中的索引
  109. const secondRowIndex = index - splitIndex.value;
  110. handlSecondRowTabs(tab, secondRowIndex);
  111. }
  112. } else {
  113. handlTabs(dataList[0], 0);
  114. }
  115. })
  116. </script>
  117. <style scoped lang="scss">
  118. .tabs-container {
  119. display: flex;
  120. flex-direction: column;
  121. width: 100%;
  122. background-color: #F6F6F6;
  123. padding: 0rpx 35rpx;
  124. }
  125. .tabs-row {
  126. width: 100%;
  127. margin-bottom: 6rpx;
  128. display: grid;
  129. grid-template-columns: repeat(5, 1fr); // 4列布局
  130. gap: 20rpx; // 网格间距
  131. }
  132. .tab-item {
  133. font-family: PingFang SC;
  134. font-size: 30rpx;
  135. font-weight: normal;
  136. line-height: 48rpx;
  137. letter-spacing: normal;
  138. color: #818181;
  139. padding: 10px 0;
  140. display: flex;
  141. align-items: center;
  142. justify-content: center;
  143. }
  144. .tab-active {
  145. font-family: PingFang SC;
  146. font-size: 30rpx;
  147. font-weight: 600;
  148. line-height: 48rpx;
  149. letter-spacing: normal;
  150. color: #1A1A1A;
  151. border-bottom: 4px solid #D94342;
  152. }
  153. /* 添加无间隔样式 */
  154. .no-gap :deep(.u-tabs__wrapper__nav__item) {
  155. margin: 0 !important;
  156. padding-left: 0 !important;
  157. padding-right: 0 !important;
  158. /* 平均分配宽度 */
  159. flex: 1 !important;
  160. text-align: center;
  161. }
  162. .childern-item {
  163. font-size: 26rpx;
  164. font-weight: 400;
  165. background-color: #f9fafb;
  166. padding: 8rpx 24rpx;
  167. margin-right: 10rpx;
  168. border-radius: 19998rpx;
  169. }
  170. .item-active {
  171. background: rgba(251, 229, 225, 1);
  172. color: rgba(221, 94, 69, 1);
  173. }
  174. .tabs-scroll {
  175. display: flex;
  176. align-items: center;
  177. margin-top: 10rpx;
  178. padding: 0 12rpx;
  179. }
  180. .tabs-scroll-box {
  181. width: 100%;
  182. overflow: hidden;
  183. overflow-x: auto;
  184. margin-top: 6rpx;
  185. }
  186. .tabs-scroll2 {
  187. margin-top: 10rpx;
  188. padding: 0 12rpx;
  189. width: max-content;
  190. display: flex;
  191. align-items: center;
  192. }
  193. .childern2-item {
  194. font-size: 22rpx;
  195. font-weight: 400;
  196. background-color: #f9fafb;
  197. padding: 6rpx 20rpx;
  198. margin-right: 10rpx;
  199. border-radius: 4rpx;
  200. color: #656565;
  201. }
  202. .item2-active {
  203. background: rgba(251, 229, 225, 1);
  204. color: rgba(221, 94, 69, 1);
  205. }
  206. </style>