cascade.vue 2.2 KB

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