index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view class="ssq adffc">
  3. <view class="box">
  4. <view class="box-top adfac">
  5. <view class="box-top-btn qx" @click="handleCancel">取消</view>
  6. <view class="box-top-title">{{title}}</view>
  7. <view class="box-top-btn qd" @click="handleConfirm">确定</view>
  8. </view>
  9. <picker-view :value="pickerValue" @change="handlePickerChange" class="picker-view">
  10. <!-- 省份列 -->
  11. <picker-view-column>
  12. <view
  13. class="picker-item"
  14. :class="{ 'selected-item': pickerValue[0] === index }"
  15. v-for="(province, index) in provinces"
  16. :key="province.name"
  17. >
  18. {{ province.name }}
  19. </view>
  20. </picker-view-column>
  21. <!-- 城市列 -->
  22. <picker-view-column>
  23. <view
  24. class="picker-item"
  25. :class="{ 'selected-item': pickerValue[1] === index }"
  26. v-for="(city, index) in cities"
  27. :key="city.name"
  28. >
  29. {{ city.name }}
  30. </view>
  31. </picker-view-column>
  32. <!-- 区县列 -->
  33. <picker-view-column>
  34. <view
  35. class="picker-item"
  36. :class="{ 'selected-item': pickerValue[2] === index }"
  37. v-for="(area, index) in areas"
  38. :key="area.name"
  39. >
  40. {{ area.name }}
  41. </view>
  42. </picker-view-column>
  43. </picker-view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import { cityData, hotCities } from './city-data.js';
  49. export default {
  50. props:{
  51. title:{
  52. typeof:String,
  53. default:'所属地区'
  54. }
  55. },
  56. data(){
  57. return {
  58. provinces:cityData,
  59. cities:[],
  60. areas:[],
  61. pickerValue:[0,0,0]
  62. }
  63. },
  64. computed:{
  65. selectedProvince:function(){
  66. return this.provinces[this.pickerValue[0]] || {}
  67. },
  68. selectedCity:function(){
  69. return this.cities[this.pickerValue[1]] || {}
  70. },
  71. selectedArea:function(){
  72. return this.areas[this.pickerValue[2]] || {}
  73. }
  74. },
  75. mounted() {
  76. this.initialize()
  77. },
  78. methods:{
  79. handlePickerChange(e){
  80. const newPickerValue = e.detail.value;
  81. const [provinceIndex, cityIndex, areaIndex] = newPickerValue;
  82. const oldProvinceIndex = this.pickerValue[0];
  83. const oldCityIndex = this.pickerValue[1];
  84. if (provinceIndex !== oldProvinceIndex) {
  85. this.cities = this.provinces[provinceIndex].children;
  86. this.areas = this.cities[0]?.children || [];
  87. this.pickerValue = [provinceIndex, 0, 0];
  88. } else if (cityIndex !== oldCityIndex) {
  89. // 更新地区列表,并将地区索引重置为0
  90. this.areas = this.cities[cityIndex]?.children || [];
  91. this.pickerValue = [provinceIndex, cityIndex, 0];
  92. } else {
  93. this.pickerValue = newPickerValue;
  94. }
  95. },
  96. initialize(){
  97. this.cities = this.provinces[0]?.children || [];
  98. this.areas = this.cities[0]?.children || [];
  99. },
  100. handleCancel(){
  101. this.$emit('cancel')
  102. },
  103. handleConfirm(){
  104. this.$emit('confirm',this.pickerValue)
  105. },
  106. }
  107. }
  108. </script>
  109. <style scoped lang="scss">
  110. .ssq{
  111. position: fixed;
  112. left: 0;
  113. right: 0;
  114. top: 0;
  115. bottom: 0;
  116. background: rgba(0, 0, 0, .5);
  117. justify-content: flex-end;
  118. z-index: 1001;
  119. .box{
  120. background: #FFFFFF;
  121. border-radius: 24rpx 24rpx 0 0;
  122. &-top{
  123. width: 100%;
  124. height: 84rpx;
  125. &-btn{
  126. width: 120rpx;
  127. font-size: 30rpx;
  128. text-align: center;
  129. line-height: 42rpx;
  130. &.qx{
  131. color: #909193;
  132. }
  133. &.qd{
  134. color: #3c9cff;
  135. }
  136. }
  137. &-title{
  138. flex: 1;
  139. font-size: 32rpx;
  140. color: #303133;
  141. text-align: center;
  142. line-height: 42rpx;
  143. }
  144. }
  145. }
  146. }
  147. .picker-view {
  148. width: 100%;
  149. height: 380rpx;
  150. margin: 40rpx 0 80rpx;
  151. }
  152. .picker-item {
  153. display: flex;
  154. align-items: center;
  155. justify-content: center;
  156. font-size: 36rpx;
  157. color: #303133;
  158. transition: all 0.2s;
  159. &.selected-item {
  160. font-size: 36rpx;
  161. font-weight: bold;
  162. color: #303133;
  163. }
  164. }
  165. </style>