index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <view class="">
  3. <view class="choose-tab" :style="{ position }">
  4. <view class="tab-list">
  5. <view
  6. class="tab"
  7. v-for="(item, index) in tabList"
  8. :class="[chooseList[index] ? 'active-tab' : '']"
  9. :key="index"
  10. @click="handleTab(item, index)"
  11. >
  12. <view>
  13. {{ chooseList[index].cateName || item.cateName }}
  14. </view>
  15. <view>
  16. <!-- {{ active }} -->
  17. <u-icon
  18. name="arrow-down"
  19. size="12px"
  20. v-if="active !== index || !show"
  21. ></u-icon>
  22. <u-icon
  23. v-if="active === index && show"
  24. name="arrow-up"
  25. size="12px"
  26. ></u-icon>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="box" v-show="show">
  31. <view v-show="show" class="first">
  32. <view
  33. :class="[levelActive[0] == item.id ? 'active' : '', 'item']"
  34. v-for="(item, index) in tabList[active].child"
  35. :key="index"
  36. @click="choose(item, 1)"
  37. >
  38. {{ item.cateName }}
  39. </view>
  40. </view>
  41. <view v-show="show" v-if="level > 1" class="second">
  42. <view
  43. :class="[levelActive[1] == item.id ? 'activeL2' : '', 'item']"
  44. v-for="(item, index) in clickObj.child"
  45. :key="index"
  46. @click="choose(item, 2)"
  47. >
  48. {{ item.cateName }}
  49. </view>
  50. </view>
  51. <view v-show="show" v-if="level > 2" class="third"> </view>
  52. </view>
  53. </view>
  54. <view
  55. class="choose-modal"
  56. :style="{ background: showModal ? 'rgba(0, 0, 0, .4)' : 'none' }"
  57. v-show="show"
  58. @click="show = false"
  59. >
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. export default {
  65. props: {
  66. position: {
  67. type: String,
  68. default: "fixed",
  69. },
  70. showModal: {
  71. type: Boolean,
  72. default: true,
  73. },
  74. tabList: {
  75. type: Array,
  76. default: () => {
  77. return [];
  78. },
  79. },
  80. level: {
  81. type: Number,
  82. default: 1,
  83. },
  84. },
  85. data() {
  86. return {
  87. show: false,
  88. chooseList: ["", "", "", ""],
  89. list: [],
  90. clickObj: {},
  91. levelActive: [null, null, null],
  92. active: null,
  93. secondList: [],
  94. thirdList: [],
  95. };
  96. },
  97. methods: {
  98. handleTab(item, index) {
  99. this.active = index;
  100. this.clickObj = {};
  101. // 如果存在已经选择过的数据
  102. if (this.chooseList[index]) {
  103. for (const e of this.tabList[index].child) {
  104. // 根据ID定位到当前选择的对象
  105. if (e.child && e.child.length) {
  106. for (const i of e.child) {
  107. if (i.id == this.chooseList[index].id) {
  108. this.clickObj = e;
  109. this.levelActive[1] = i.id;
  110. }
  111. }
  112. } else {
  113. if (e.id == this.chooseList[index].id) {
  114. this.levelActive[0] = e.id;
  115. }
  116. }
  117. }
  118. }
  119. this.$nextTick(() => {
  120. this.show = !this.show;
  121. });
  122. },
  123. choose(item, level) {
  124. // console.log("item", item);
  125. this.levelActive[level - 1] = item.id;
  126. this.clickObj = item;
  127. // 没有子级直接提交当前
  128. if (level == this.level || !item.child || !item.child.length) {
  129. this.show = false;
  130. // 重复选择则取消
  131. // console.log(
  132. // "this.chooseList",
  133. // this.chooseList,
  134. // "this.active",
  135. // this.active
  136. // );
  137. let id = this.chooseList[this.active]?.id || "";
  138. if (item.id == id) {
  139. this.chooseList[this.active] = "";
  140. this.levelActive = [null, null, null];
  141. } else {
  142. this.chooseList[this.active] = item;
  143. }
  144. this.$emit("choose", this.chooseList);
  145. }
  146. },
  147. },
  148. };
  149. </script>
  150. <style lang="scss" scoped>
  151. .choose-modal {
  152. position: fixed;
  153. top: 0;
  154. left: 0;
  155. width: 100%;
  156. height: 100%;
  157. background: rgba(0, 0, 0, 0.4);
  158. z-index: 99;
  159. }
  160. .choose-tab {
  161. // position: fixed;
  162. top: 0;
  163. left: 0;
  164. background-color: #fafafa;
  165. overflow: hidden;
  166. border-radius: 0rpx 0rpx 16rpx 16rpx;
  167. width: 100%;
  168. z-index: 100;
  169. .tab-list {
  170. display: flex;
  171. align-items: center;
  172. padding: 20rpx;
  173. width: 100vw;
  174. overflow-x: scroll;
  175. background: #fff;
  176. .tab {
  177. display: flex;
  178. justify-content: center;
  179. word-break: keep-all;
  180. align-items: center;
  181. background: #f0f0f0;
  182. border-radius: 8rpx 8rpx 8rpx 8rpx;
  183. height: 40rpx;
  184. line-height: 40rpx;
  185. color: #222222;
  186. font-size: 24rpx;
  187. padding: 0 10rpx;
  188. margin-right: 16rpx;
  189. }
  190. }
  191. .box {
  192. background: #fff;
  193. display: flex;
  194. justify-content: start;
  195. .item {
  196. max-height: 600rpx;
  197. font-size: 30rpx;
  198. overflow: auto;
  199. color: #222222;
  200. padding: 14rpx 24rpx;
  201. width: 100%/3;
  202. }
  203. .first {
  204. flex: 1;
  205. background-color: #ffffff;
  206. }
  207. .second {
  208. flex: 1;
  209. background-color: #f7f7f7;
  210. }
  211. .third {
  212. flex: 1;
  213. background-color: #f2f2f2;
  214. }
  215. }
  216. .active-tab {
  217. color: $uni-color-primary !important;
  218. background-color: rgba(246,229,229,.9) !important;
  219. }
  220. .active {
  221. color: $uni-color-primary !important;
  222. background-color: #f7f7f7;
  223. position: relative;
  224. &::before {
  225. content: "";
  226. position: absolute;
  227. top: 18rpx;
  228. left: 10rpx;
  229. width: 8rpx;
  230. height: 35rpx;
  231. transform: translateX(-50%);
  232. border-radius: 2rpx;
  233. background: $uni-color-primary;
  234. }
  235. }
  236. .activeL2 {
  237. color: $uni-color-primary !important;
  238. background-color: #f2f2f2;
  239. position: relative;
  240. &::before {
  241. content: "";
  242. position: absolute;
  243. top: 18rpx;
  244. left: 10rpx;
  245. width: 8rpx;
  246. height: 35rpx;
  247. transform: translateX(-50%);
  248. border-radius: 2rpx;
  249. background: $uni-color-primary;
  250. }
  251. }
  252. .activeL3 {
  253. color: $uni-color-primary !important;
  254. }
  255. }
  256. </style>