pickerAddress.vue 2.1 KB

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