index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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' : '']"
  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"
  21. ></u-icon>
  22. <u-icon
  23. v-if="active === index"
  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. background: #fff;
  174. .tab {
  175. display: flex;
  176. justify-content: center;
  177. align-items: center;
  178. background: #f0f0f0;
  179. border-radius: 8rpx 8rpx 8rpx 8rpx;
  180. height: 40rpx;
  181. line-height: 40rpx;
  182. color: #222222;
  183. font-size: 24rpx;
  184. padding: 0 10rpx;
  185. margin-right: 16rpx;
  186. }
  187. }
  188. .box {
  189. background: #fff;
  190. display: flex;
  191. justify-content: start;
  192. padding: 10rpx;
  193. .item {
  194. max-height: 600rpx;
  195. font-size: 30rpx;
  196. overflow: auto;
  197. color: #222222;
  198. padding: 14rpx 24rpx;
  199. width: 100%/3;
  200. }
  201. .first {
  202. flex: 1;
  203. background-color: #ffffff;
  204. }
  205. .second {
  206. flex: 1;
  207. background-color: #f7f7f7;
  208. }
  209. .third {
  210. flex: 1;
  211. background-color: #f2f2f2;
  212. }
  213. }
  214. .active {
  215. color: $uni-color-primary !important;
  216. background-color: #f7f7f7;
  217. }
  218. .activeL2 {
  219. color: $uni-color-primary !important;
  220. background-color: #f2f2f2;
  221. }
  222. .activeL3 {
  223. color: $uni-color-primary !important;
  224. }
  225. }
  226. </style>