pickerAddress.vue 2.2 KB

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