city.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <uni-popup ref="popupRef" type="bottom" border-radius="10px 10px 0 0">
  3. <!-- :indicator-style="indicatorStyle" -->
  4. <div class="city-box">
  5. <div class="city-picker-btn">
  6. <text @click.stop="cancel()">取消</text>
  7. <text @click.stop="confirm()">确定</text>
  8. </div>
  9. <picker-view indicator-class="indicator-class" :value="value||[]" class="picker-view" @change="bindChange">
  10. <picker-view-column>
  11. <view class="item" v-for="(item,index) in sheng" :key="index">{{item.label}}</view>
  12. </picker-view-column>
  13. <picker-view-column>
  14. <view class="item" v-for="(item,index) in shi" :key="index">{{item.label}}</view>
  15. </picker-view-column>
  16. <picker-view-column>
  17. <view class="item" v-for="(item,index) in qu" :key="index">{{item.label}}</view>
  18. </picker-view-column>
  19. </picker-view>
  20. </div>
  21. </uni-popup>
  22. </template>
  23. <script>
  24. import { cityJson } from "./json.js"
  25. export default {
  26. name: "city",
  27. props: {
  28. code: {
  29. type: String | Array,
  30. default: null
  31. }
  32. },
  33. data() {
  34. return {
  35. codeValue: [],
  36. labelValue:'',
  37. value: [0, 0, 0],
  38. sheng: [cityJson[16]],
  39. shi: [],
  40. qu: []
  41. }
  42. },
  43. watch: {
  44. value: {
  45. handler(newV, oldV) {
  46. const indexArr = newV || []
  47. this.shi = [];
  48. this.qu = [];
  49. // 省
  50. const sh_l = indexArr[0] || 0;
  51. const sheng_ = this.sheng[sh_l];
  52. let shi_ = null;
  53. let qu_ = null;
  54. let code = [sheng_.value];
  55. let label = sheng_.label;
  56. if (sheng_.children && sheng_.children.length > 0) {
  57. // 市
  58. this.shi = sheng_.children;
  59. const s_l = indexArr[1] || 0;
  60. shi_ = this.shi[s_l]
  61. code.push(shi_.value)
  62. label = `${label}/${shi_.label}`
  63. }
  64. if (shi_ && shi_.children && shi_.children.length > 0) {
  65. // 区
  66. this.qu = shi_.children;
  67. const q_l = indexArr[2] || 0;
  68. const qu_ = this.qu[q_l]
  69. code.push(qu_.value)
  70. label = `${label}/${qu_.label}`
  71. }
  72. this.codeValue = code;
  73. this.labelValue = label;
  74. },
  75. immediate: true,
  76. deep: true
  77. }
  78. },
  79. created() {
  80. },
  81. mounted() {
  82. },
  83. methods: {
  84. open(){
  85. this.$refs.popupRef.open()
  86. },
  87. bindChange(val) {
  88. const { value } = val.detail;
  89. console.log("bindChange = ", value, this.value)
  90. if (value[0] !== this.value[0]) {
  91. this.value = [value[0], 0, 0];
  92. return
  93. }
  94. if (value[1] !== this.value[1]) {
  95. this.value = [value[0], value[1], 0];
  96. return
  97. }
  98. this.value = value;
  99. },
  100. confirm(){
  101. this.$emit("update:code", this.codeValue);
  102. this.$emit("cityName", this.labelValue);
  103. this.$emit("onConfirm")
  104. this.cancel();
  105. },
  106. cancel(){
  107. this.$refs.popupRef.close()
  108. }
  109. }
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .uni-popup {
  114. z-index: 1001;
  115. .city-box {
  116. height: 55vh;
  117. background-color: #fff;
  118. border-radius: 30rpx 30rpx 0 0;
  119. .city-picker-btn {
  120. height: 100rpx;
  121. display: flex;
  122. justify-content: space-between;
  123. align-items: center;
  124. padding: 0 30rpx;
  125. }
  126. .picker-view {
  127. height: calc(100% - 100rpx);
  128. text-align: center;
  129. }
  130. }
  131. }
  132. /deep/ .indicator-class {
  133. height: 100rpx;
  134. }
  135. /deep/ .uni-picker-view-content {
  136. .item {
  137. text-align: center;
  138. line-height: 100rpx;
  139. }
  140. }
  141. </style>