wangding-pickerAddress.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 AllAddress from './data.js';
  6. let selectVal = ['', '', ''];
  7. export default {
  8. data() {
  9. return {
  10. value: [0, 0, 0],
  11. array: [],
  12. index: 0
  13. };
  14. },
  15. created() {
  16. this.initSelect();
  17. },
  18. methods: {
  19. // 初始化地址选项
  20. initSelect() {
  21. this.updateSourceDate() // 更新源数据
  22. .updateAddressDate() // 更新结果数据
  23. .$forceUpdate(); // 触发双向绑定
  24. },
  25. // 地址控件改变控件
  26. columnchange(d) {
  27. this.updateSelectIndex(d.detail.column, d.detail.value) // 更新选择索引
  28. .updateSourceDate() // 更新源数据
  29. .updateAddressDate() // 更新结果数据
  30. .$forceUpdate(); // 触发双向绑定
  31. },
  32. /**
  33. * 更新源数据
  34. * */
  35. updateSourceDate() {
  36. this.array = [];
  37. this.array[0] = AllAddress.map(obj => {
  38. console.log(obj, '123456');
  39. return {
  40. name: obj.label
  41. };
  42. });
  43. this.array[1] = AllAddress[this.value[0]].children.map(obj => {
  44. return {
  45. name: obj.label
  46. };
  47. });
  48. if (AllAddress[this.value[0]].children[this.value[1]].children) {
  49. this.array[2] = AllAddress[this.value[0]].children[this.value[1]].children.map(obj => {
  50. return {
  51. name: obj.label
  52. };
  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></style>