pickerAddress.vue 3.1 KB

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