pickerAddress.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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) {
  36. console.log('selectValue',this.selectValue);
  37. this.value = this.selectValue
  38. this.initSelect()
  39. },
  40. immediate: true
  41. }
  42. },
  43. methods: {
  44. // 初始化地址选项
  45. initSelect() {
  46. this.updateSourceDate() // 更新源数据
  47. .updateAddressDate() // 更新结果数据
  48. .$forceUpdate() // 触发双向绑定
  49. },
  50. // 地址控件改变控件
  51. columnchange(d) {
  52. this.updateSelectIndex(d.detail.column, d.detail.value) // 更新选择索引
  53. .updateSourceDate() // 更新源数据
  54. .updateAddressDate() // 更新结果数据
  55. .$forceUpdate() // 触发双向绑定
  56. },
  57. /**
  58. * 更新源数据
  59. * */
  60. updateSourceDate() {
  61. // console.log(AllAddress, '>>>>>>>AllAddress');
  62. this.array = []
  63. // this.array[0] = AllAddress.map(obj => {
  64. // return { name: obj.name }
  65. // })
  66. // this.array[1] = AllAddress[this.value[0]].city.map(obj => {
  67. // return { name: obj.name }
  68. // })
  69. // this.array[2] = AllAddress[this.value[0]].city[this.value[1]].area.map(obj => {
  70. // return { name: obj }
  71. // })
  72. this.array[0] = AllAddress.map(obj => {
  73. return {
  74. name: obj.name,
  75. code: obj.code
  76. }
  77. })
  78. this.array[1] = AllAddress[this.value[0]].children.map(obj => {
  79. return {
  80. name: obj.name,
  81. code: obj.code
  82. }
  83. })
  84. this.array[2] = AllAddress[this.value[0]].children[this.value[1]].children.map(obj => {
  85. return {
  86. name: obj.name,
  87. code: obj.code
  88. }
  89. })
  90. return this
  91. },
  92. /**
  93. * 更新索引
  94. * */
  95. updateSelectIndex(column, value) {
  96. let arr = JSON.parse(JSON.stringify(this.value))
  97. arr[column] = value
  98. if (column === 0) {
  99. arr[1] = 0
  100. arr[2] = 0
  101. }
  102. if (column === 1) {
  103. arr[2] = 0
  104. }
  105. this.value = arr
  106. return this
  107. },
  108. /**
  109. * 更新结果数据
  110. * */
  111. updateAddressDate() {
  112. selectVal[0] = this.array[0][this.value[0]].name
  113. selectVal[1] = this.array[1][this.value[1]].name
  114. selectVal[2] = this.array[2][this.value[2]].name
  115. selectCode[0] = this.array[0][this.value[0]].code
  116. selectCode[1] = this.array[1][this.value[1]].code
  117. selectCode[2] = this.array[2][this.value[2]].code
  118. return this
  119. },
  120. /**
  121. * 点击确定
  122. * */
  123. bindPickerChange(e) {
  124. this.$emit('change', {
  125. index: this.value,
  126. data: selectVal,
  127. code: selectCode
  128. })
  129. return this
  130. }
  131. }
  132. }
  133. </script>
  134. <style></style>