pickerAddress.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <!-- 地址选择器组件 -->
  3. <view>
  4. <picker @change="bindPickerChange" @columnchange="columnchange" :range="array" range-key="name" :value="value"
  5. mode="multiSelector">
  6. <slot></slot>
  7. </picker>
  8. </view>
  9. </template>
  10. <script>
  11. // import AllAddress from './areadata.js'
  12. import AllAddress from './address.js'
  13. let selectVal = ['', '', '']
  14. let selectCode = ['', '', '']
  15. export default {
  16. data() {
  17. return {
  18. value: [0, 0, 0],
  19. array: [],
  20. index: 0
  21. }
  22. },
  23. props: {
  24. selectValue: {
  25. type: Array,
  26. default: [0, 0, 0]
  27. }
  28. },
  29. created() {
  30. this.value = this.selectValue
  31. this.initSelect()
  32. },
  33. watch: {
  34. selectValue: {
  35. handler(newVal,old) {
  36. if(newVal!== old){
  37. console.log('newVal,old',newVal,old);
  38. this.value = this.selectValue
  39. this.initSelect()
  40. }
  41. },
  42. immediate: true
  43. }
  44. },
  45. methods: {
  46. // 初始化地址选项
  47. initSelect() {
  48. this.updateSourceDate() // 更新源数据
  49. .updateAddressDate() // 更新结果数据
  50. .$forceUpdate() // 触发双向绑定
  51. },
  52. // 地址控件改变控件
  53. columnchange(d) {
  54. this.updateSelectIndex(d.detail.column, d.detail.value) // 更新选择索引
  55. .updateSourceDate() // 更新源数据
  56. .updateAddressDate() // 更新结果数据
  57. .$forceUpdate() // 触发双向绑定
  58. },
  59. /**
  60. * 更新源数据
  61. * */
  62. updateSourceDate() {
  63. // console.log(AllAddress, '>>>>>>>AllAddress');
  64. this.array = []
  65. // this.array[0] = AllAddress.map(obj => {
  66. // return { name: obj.name }
  67. // })
  68. // this.array[1] = AllAddress[this.value[0]].city.map(obj => {
  69. // return { name: obj.name }
  70. // })
  71. // this.array[2] = AllAddress[this.value[0]].city[this.value[1]].area.map(obj => {
  72. // return { name: obj }
  73. // })
  74. this.array[0] = AllAddress.map(obj => {
  75. return {
  76. name: obj.name,
  77. code: obj.code
  78. }
  79. })
  80. this.array[1] = AllAddress[this.value[0]].children.map(obj => {
  81. return {
  82. name: obj.name,
  83. code: obj.code
  84. }
  85. })
  86. this.array[2] = AllAddress[this.value[0]].children[this.value[1]].children.map(obj => {
  87. return {
  88. name: obj.name,
  89. code: obj.code
  90. }
  91. })
  92. return this
  93. },
  94. /**
  95. * 更新索引
  96. * */
  97. updateSelectIndex(column, value) {
  98. let arr = JSON.parse(JSON.stringify(this.value))
  99. arr[column] = value
  100. if (column === 0) {
  101. arr[1] = 0
  102. arr[2] = 0
  103. }
  104. if (column === 1) {
  105. arr[2] = 0
  106. }
  107. this.value = arr
  108. return this
  109. },
  110. /**
  111. * 更新结果数据
  112. * */
  113. updateAddressDate() {
  114. selectVal[0] = this.array[0][this.value[0]].name
  115. selectVal[1] = this.array[1][this.value[1]].name
  116. selectVal[2] = this.array[2][this.value[2]].name
  117. selectCode[0] = this.array[0][this.value[0]].code
  118. selectCode[1] = this.array[1][this.value[1]].code
  119. selectCode[2] = this.array[2][this.value[2]].code
  120. return this
  121. },
  122. /**
  123. * 点击确定
  124. * */
  125. bindPickerChange(e) {
  126. this.$emit('change', {
  127. index: this.value,
  128. data: selectVal,
  129. code: selectCode
  130. })
  131. return this
  132. }
  133. }
  134. }
  135. </script>
  136. <style></style>