pickerAddress.vue 2.3 KB

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