wangding-pickerAddress.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 './city-picker.data.js';
  6. let selectVal = [
  7. {
  8. name: '安徽省',
  9. code: '340000'
  10. },
  11. {
  12. name: '合肥市',
  13. code: '340100'
  14. },
  15. {
  16. name: '瑶海区',
  17. code: '340102'
  18. }
  19. ];
  20. export default {
  21. data() {
  22. return {
  23. value: [0, 0, 0],
  24. code:['340000','340100','340102'],
  25. array: [],
  26. index: 0
  27. };
  28. },
  29. created() {
  30. this.initSelect();
  31. },
  32. methods: {
  33. // 初始化地址选项
  34. initSelect() {
  35. this.updateSourceDate() // 更新源数据
  36. .updateAddressDate() // 更新结果数据
  37. .$forceUpdate(); // 触发双向绑定
  38. },
  39. // 地址控件改变控件
  40. columnchange(d) {
  41. this.updateSelectIndex(d.detail.column, d.detail.value)// 更新选择索引
  42. .updateAddressDate(d.detail.column) // 更新结果数据
  43. .updateSourceDate() // 更新源数据
  44. .$forceUpdate(); // 触发双向绑定
  45. },
  46. /**
  47. * 更新源数据
  48. * */
  49. updateSourceDate() {
  50. let obj = this;
  51. obj.array = [[], [], []];
  52. for (let i in AllAddress[86]) {
  53. // 循环对象中的数组数据
  54. AllAddress[86][i].forEach(e => {
  55. obj.array[0].push({
  56. name: e.address,
  57. code: e.code
  58. });
  59. });
  60. }
  61. for (let i in AllAddress[selectVal[0].code]) {
  62. // 循环对象中的数组数据
  63. obj.array[1].push({
  64. name: AllAddress[selectVal[0].code][i],
  65. code: i
  66. });
  67. }
  68. for (let i in AllAddress[selectVal[1].code]) {
  69. // 循环对象中的数组数据
  70. obj.array[2].push({
  71. name: AllAddress[selectVal[1].code][i],
  72. code: i
  73. });
  74. }
  75. return obj;
  76. },
  77. /**
  78. * 更新索引
  79. * */
  80. updateSelectIndex(column, value) {
  81. let arr = JSON.parse(JSON.stringify(this.value));
  82. arr[column] = value;
  83. if (column === 0) {
  84. arr[1] = 0;
  85. arr[2] = 0;
  86. }
  87. if (column === 1) {
  88. arr[2] = 0;
  89. }
  90. this.value = arr;
  91. return this;
  92. },
  93. /**
  94. * 更新结果数据
  95. * */
  96. updateAddressDate(column) {
  97. // 保存一级对象数据
  98. selectVal[0] = this.array[0][this.value[0]];
  99. if (column === 0) {
  100. // 查询二级对象数据
  101. let arr1 = AllAddress[selectVal[0].code];
  102. for(let i in arr1){
  103. selectVal[1] = {
  104. name: arr1[i],
  105. code: i
  106. };
  107. break
  108. }
  109. // 查询三级对象数据
  110. let arr2 = AllAddress[selectVal[1].code];
  111. for(let i in arr2){
  112. selectVal[2] = {
  113. name: arr2[i],
  114. code: i
  115. };
  116. break
  117. }
  118. }
  119. if (column === 1) {
  120. // 保存当前选中的二级对象数据
  121. selectVal[1] = this.array[1][this.value[1]]
  122. console.log(selectVal[1]);
  123. // 查询三级数据
  124. let arr2 = AllAddress[selectVal[1].code];
  125. for(let i in arr2){
  126. selectVal[2] = {
  127. name: arr2[i],
  128. code: i
  129. };
  130. break
  131. }
  132. }
  133. return this;
  134. },
  135. /**
  136. * 点击确定
  137. * */
  138. bindPickerChange(e) {
  139. this.$emit('change', {
  140. index: this.value,
  141. data: selectVal
  142. });
  143. return this;
  144. }
  145. }
  146. };
  147. </script>
  148. <style></style>