wangding-pickerAddress.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: [0,0,0],
  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. this.updateSelectIndex(d.detail.column, d.detail.value) // 更新选择索引
  30. .updateSourceDate() // 更新源数据
  31. .updateAddressDate() // 更新结果数据
  32. .$forceUpdate() // 触发双向绑定
  33. },
  34. /**
  35. * 更新源数据
  36. * */
  37. updateSourceDate() {
  38. this.array = []
  39. this.array[0] = AllAddress.map(obj => {
  40. return {
  41. name: obj.name
  42. }
  43. })
  44. this.array[1] = AllAddress[this.value[0]].city.map(obj => {
  45. return {
  46. name: obj.name
  47. }
  48. })
  49. this.array[2] = AllAddress[this.value[0]].city[this.value[1]].area.map(obj => {
  50. return {
  51. name: obj
  52. }
  53. })
  54. return this
  55. },
  56. /**
  57. * 更新索引
  58. * */
  59. updateSelectIndex(column, value){
  60. let arr = JSON.parse(JSON.stringify(this.value))
  61. arr[column] = value
  62. if(column === 0 ) {
  63. arr[1] = 0
  64. arr[2] = 0
  65. }
  66. if(column === 1 ) {
  67. arr[2] = 0
  68. }
  69. this.value = arr
  70. return this
  71. },
  72. /**
  73. * 更新结果数据
  74. * */
  75. updateAddressDate() {
  76. selectVal[0] = this.array[0][this.value[0]].name
  77. selectVal[1] = this.array[1][this.value[1]].name
  78. selectVal[2] = this.array[2][this.value[2]].name
  79. return this
  80. },
  81. /**
  82. * 点击确定
  83. * */
  84. bindPickerChange(e) {
  85. this.$emit('change', {
  86. index: this.value,
  87. data: selectVal
  88. })
  89. return this
  90. }
  91. }
  92. }
  93. </script>
  94. <style>
  95. </style>