hu-pickerAddress.vue 2.8 KB

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