lazyPicker.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view>
  3. <uni-popup ref="popup" type="bottom" @change="handleChange">
  4. <view class="wrap">
  5. <view class="title">请选择社区</view>
  6. <image class="close" src="@/static/images/icons/close.png" @click="close"></image>
  7. <scroll-view class="select-scroll" scroll-x show-scrollbar>
  8. <view class="select-tab" :class="{active:idx === curTab}" v-for="(tab,idx) in tabList" :key="tab.id"
  9. @click="tabClick(tab,idx)">
  10. {{tab.label}}
  11. </view>
  12. </scroll-view>
  13. <view class="cont-wrap">
  14. <scroll-view class="content" :class="{active:listIdx === curTab}" scroll-y v-for="(list,listIdx) in levels"
  15. :key="list">
  16. <view class="list-item" v-for="(item,index) in contList[listIdx]" :key="item.id"
  17. @click="itemClick(item,index)">
  18. <view class="item-text">{{item.label}}</view>
  19. <view class="check" :class="{checked:listIdx === lastTab && curItem === index}"></view>
  20. </view>
  21. </scroll-view>
  22. </view>
  23. </view>
  24. </uni-popup>
  25. </view>
  26. </template>
  27. <script>
  28. import { getUnitTree } from '@/api/business'
  29. export default {
  30. name: "LazyPicker",
  31. props: {
  32. open: {
  33. type: Boolean,
  34. required: true,
  35. default: false,
  36. }
  37. },
  38. data() {
  39. return {
  40. parentId: null,
  41. levels: 5,
  42. curTab: 0,
  43. lastTab: null,
  44. tabList: [
  45. { id: 0, label: '请选择' },
  46. ],
  47. curItem: null,
  48. contList: [],
  49. };
  50. },
  51. created() {
  52. getUnitTree().then(res => {
  53. if (res.code !== 200) return
  54. this.contList.push(res.data)
  55. this.levels = Number(res.level)
  56. })
  57. },
  58. watch: {
  59. open(value) {
  60. value ? this.$refs.popup.open() : this.$refs.popup.close()
  61. },
  62. },
  63. methods: {
  64. handleChange(e) {
  65. this.$emit('change', e.show)
  66. },
  67. close() {
  68. this.$refs.popup.close()
  69. },
  70. tabClick(tab, index) {
  71. this.curTab = index
  72. },
  73. itemClick(item, index) {
  74. this.curItem = index
  75. this.tabList[this.curTab] = item
  76. this.$emit('selected', item)
  77. this.parentId = item.id
  78. getUnitTree(this.parentId).then(({ code, data }) => {
  79. if (code !== 200) return
  80. this.lastTab = this.curTab
  81. if (data.length > 0) {
  82. // this.curItem = null //重置
  83. let nextTab = this.curTab + 1
  84. if (this.tabList[nextTab]) {
  85. this.tabList[nextTab] = { id: index, label: '请选择' }
  86. this.tabList.splice(nextTab + 1)
  87. this.curTab += 1
  88. this.contList[this.curTab] = data
  89. } else {
  90. this.tabList.push({ id: index, label: '请选择' })
  91. this.curTab += 1
  92. this.contList[this.curTab] = data
  93. }
  94. } else {
  95. this.tabList.splice(this.curTab + 1)
  96. }
  97. })
  98. }
  99. },
  100. }
  101. </script>
  102. <style lang="scss">
  103. .wrap {
  104. font-size: 14px;
  105. width: 100%;
  106. background-color: #FFFFFF;
  107. border-top-left-radius: 10px;
  108. border-top-right-radius: 10px;
  109. position: relative;
  110. .close {
  111. height: 20px;
  112. width: 20px;
  113. position: absolute;
  114. top: 12px;
  115. right: 8px;
  116. }
  117. .title {
  118. font-size: 16px;
  119. height: 44px;
  120. line-height: 44px;
  121. text-align: center;
  122. }
  123. .select-scroll {
  124. white-space: nowrap;
  125. border-bottom: 1px solid #f8f8f8;
  126. padding: 0 5px;
  127. }
  128. .select-tab {
  129. display: inline-block;
  130. margin-left: 10px;
  131. margin-right: 10px;
  132. padding: 12px 0;
  133. text-align: center;
  134. white-space: nowrap;
  135. position: relative;
  136. &::after {
  137. content: '';
  138. height: 2px;
  139. width: 100%;
  140. position: absolute;
  141. left: 0%;
  142. bottom: 0;
  143. }
  144. }
  145. .active::after {
  146. background-color: #2979ff;
  147. }
  148. .content {
  149. height: 460px;
  150. display: none;
  151. &.active {
  152. display: block;
  153. }
  154. .list-item {
  155. padding: 12px 15px;
  156. display: flex;
  157. flex-direction: row;
  158. justify-content: space-between;
  159. }
  160. .check {
  161. margin-right: 5px;
  162. border: 2px solid #2979ff;
  163. border-left: 0;
  164. border-top: 0;
  165. height: 12px;
  166. width: 6px;
  167. transform-origin: center;
  168. transition: all 0.3s;
  169. transform: rotate(45deg);
  170. display: none;
  171. &.checked {
  172. display: block;
  173. }
  174. }
  175. }
  176. }
  177. </style>