wangding-pickerAddress.vue 2.3 KB

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