wangding-pickerAddress.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. return {
  39. name: obj.value
  40. };
  41. });
  42. this.array[1] = AllAddress[this.value[0]].children.map(obj => {
  43. return {
  44. name: obj.value
  45. };
  46. });
  47. this.array[2] = AllAddress[this.value[0]].children[this.value[1]].children.map(obj => {
  48. return {
  49. name: obj.value
  50. };
  51. });
  52. return this;
  53. },
  54. /**
  55. * 更新索引
  56. * */
  57. updateSelectIndex(column, value) {
  58. let arr = JSON.parse(JSON.stringify(this.value));
  59. arr[column] = value;
  60. if (column === 0) {
  61. arr[1] = 0;
  62. arr[2] = 0;
  63. }
  64. if (column === 1) {
  65. arr[2] = 0;
  66. }
  67. this.value = arr;
  68. return this;
  69. },
  70. /**
  71. * 更新结果数据
  72. * */
  73. updateAddressDate() {
  74. selectVal[0] = this.array[0][this.value[0]].name;
  75. selectVal[1] = this.array[1][this.value[1]].name;
  76. selectVal[2] = this.array[2][this.value[2]].name;
  77. return this;
  78. },
  79. /**
  80. * 点击确定
  81. * */
  82. bindPickerChange(e) {
  83. this.$emit('change', {
  84. index: this.value,
  85. data: selectVal
  86. });
  87. return this;
  88. }
  89. }
  90. };
  91. </script>
  92. <style></style>