selectAddress.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view>
  3. <picker :class="disabled?'pickerClass':''" ref='picker' @change="bindPickerChange" :value="multiIndex"
  4. :range="multiArray" range-key='label' mode="multiSelector" @columnchange='columnchange'>
  5. <view class="text" :style="textStyleEmpty" v-if="place=='选择地区'">
  6. {{place}}
  7. </view>
  8. <view class="text" :style="textStyle" v-else>{{edit?text:place}}</view>
  9. <slot name="icon"></slot>
  10. </picker>
  11. </view>
  12. </template>
  13. <script>
  14. var that= this
  15. import {
  16. address
  17. } from '@/components/data/data.js'
  18. import {
  19. address1
  20. } from '@/components/data/data1.js'
  21. export default {
  22. name: 'selectAddress',
  23. props: {
  24. searchPlace: {
  25. type: String,
  26. default: '全国'
  27. },
  28. edit: {
  29. value: Boolean,
  30. default: false
  31. },
  32. text: {
  33. type: String,
  34. default: ''
  35. },
  36. series: {
  37. type: String,
  38. default: '1'
  39. },
  40. textStyle: {
  41. type: Object,
  42. },
  43. disabled: {
  44. value: Boolean,
  45. default: false
  46. },
  47. },
  48. watch: {
  49. text:{
  50. handler(oldValue, newValue) {
  51. console.log(11111111)
  52. console.log(oldValue, newValue)
  53. // if (val != '') {
  54. this.place = oldValue
  55. // }
  56. console.log(this.place,this.text,this.edit)
  57. },
  58. deep:true,
  59. immediate:true,
  60. },
  61. place:{
  62. handler(oldValue, newValue) {
  63. // console.log(11111111)
  64. // console.log(oldValue, newValue)
  65. // // if (val != '') {
  66. // this.place = oldValue
  67. // // }
  68. // console.log(this.place,this.text,this.edit)
  69. },
  70. deep:true,
  71. immediate:true,
  72. },
  73. },
  74. data() {
  75. return {
  76. check: false,
  77. place: '',
  78. textStyleEmpty: {
  79. "color": '#C5CAD4',
  80. "font-size": '32rpx'
  81. },
  82. multiIndex: [0, 0, 0],
  83. addressCopy: null,
  84. multiArray: [
  85. [],
  86. [],
  87. []
  88. ],
  89. }
  90. },
  91. mounted() {
  92. if(!this.edit&&!this.text){
  93. this.place = this.searchPlace
  94. }
  95. // console.log("this.series", this.series)
  96. if (this.series == 3) {
  97. this.addressCopy = JSON.parse(JSON.stringify(address1))
  98. this.addressCopy.shift()
  99. } else {
  100. this.addressCopy = JSON.parse(JSON.stringify(address))
  101. }
  102. // console.log("this.addressCopy", this.addressCopy)
  103. for (let i = 0; i < this.addressCopy.length; i++) {
  104. this.multiArray[0].push(this.addressCopy[i])
  105. }
  106. this.multiArray[1] = this.addressCopy[0].child
  107. this.multiArray[2] = this.addressCopy[0].child[0].child
  108. // this.$emit('selectAddress', '')
  109. },
  110. methods: {
  111. columnchange(e) {
  112. console.log('picker发送选择改变', e.detail)
  113. this.multiIndex[e.detail.column] = e.detail.value
  114. console.log("this.multiIndex", this.multiIndex)
  115. switch (e.detail.column) {
  116. case 0: //拖动第1列
  117. this.multiArray[1] = this.addressCopy[e.detail.value].child
  118. this.multiArray[2] = this.multiArray[1][0].child
  119. console.log(this.multiArray[1])
  120. this.multiIndex.splice(1, 1, 0)
  121. this.multiIndex.splice(2, 1, 0)
  122. break
  123. case 1: //拖动第2列
  124. this.multiArray[2] = this.multiArray[1][this.multiIndex[1]].child
  125. this.multiIndex.splice(2, 1, 0)
  126. break
  127. }
  128. this.$forceUpdate()
  129. },
  130. bindPickerChange: function(e) {
  131. this.check = false
  132. console.log('picker发送选择改变,携带值为', e.detail.value)
  133. this.index = e.detail.value
  134. console.log(this.multiArray)
  135. //判断全部
  136. let _address = ''
  137. let _showVal = ''
  138. if (this.multiArray[0][e.detail.value[0]].label == '全国') {
  139. _address = ''
  140. this.place = '全国'
  141. } else if (this.multiArray[1][e.detail.value[1]]
  142. .label == '全部') {
  143. _address = this.multiArray[0][e.detail.value[0]].label
  144. this.place = this.multiArray[0][e.detail.value[0]].label
  145. } else if (this.multiArray[2][e.detail.value[2]].label == '全部') {
  146. _address = this.multiArray[0][e.detail.value[0]].label + this.multiArray[1][e.detail.value[1]]
  147. .label
  148. this.place = this.multiArray[1][e.detail.value[1]].label
  149. } else {
  150. if(this.multiArray[0][e.detail.value[0]].label=='北京'||this.multiArray[0][e.detail.value[0]].label=='天津'||this.multiArray[0][e.detail.value[0]].label=='上海'||this.multiArray[0][e.detail.value[0]].label=='重庆'){
  151. _address = this.multiArray[1][e.detail.value[1]]
  152. .label + this.multiArray[2][e.detail.value[2]].label
  153. }else{
  154. _address = this.multiArray[0][e.detail.value[0]].label + this.multiArray[1][e.detail.value[1]]
  155. .label + this.multiArray[2][e.detail.value[2]].label
  156. }
  157. this.place = this.multiArray[2][e.detail.value[2]].label
  158. this.check = true
  159. }
  160. if (this.series == 3) {
  161. if (!_address) {
  162. this.place = '全国'
  163. }
  164. if (this.check) {
  165. this.place = _address
  166. } else {
  167. this.place = this.searchPlace
  168. }
  169. }
  170. console.log("_showVal", this.place)
  171. console.log("_address", _address)
  172. console.log("this.check", this.check)
  173. this.$emit('selectAddress', {
  174. showVal: this.place,
  175. address: _address,
  176. check: this.check
  177. })
  178. }
  179. },
  180. }
  181. </script>
  182. <style lang="scss" scoped>
  183. .text {
  184. display: inline-block;
  185. margin: 0;
  186. }
  187. </style>