pick-regions.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <picker
  3. mode="multiSelector"
  4. :value="multiIndex"
  5. :range="multiArray"
  6. @change="handleValueChange"
  7. @columnchange="handleColumnChange"
  8. >
  9. <slot></slot>
  10. </picker>
  11. </template>
  12. <script>
  13. const CHINA_REGIONS = require('./regions.json');
  14. export default {
  15. props: {
  16. defaultRegions: {
  17. type: Array,
  18. default() {
  19. return [];
  20. }
  21. },
  22. defaultRegionCode: {
  23. type: String
  24. },
  25. defaultRegion: [String, Array],
  26. value: [String, Array]
  27. },
  28. data() {
  29. return {
  30. cityArr: CHINA_REGIONS[0].childs,
  31. districtArr: CHINA_REGIONS[0].childs[0].childs,
  32. multiIndex: [0, 0, 0],
  33. isInitMultiArray: true
  34. };
  35. },
  36. watch: {
  37. defaultRegion: {
  38. handler(region, oldRegion) {
  39. if (Array.isArray(region)) {
  40. // 避免传的是字面量的时候重复触发
  41. oldRegion = oldRegion || [];
  42. if (region.join('') !== oldRegion.join('')) {
  43. this.handleDefaultRegion(region);
  44. }
  45. } else if (region && region.length == 6) {
  46. this.handleDefaultRegion(region);
  47. } else {
  48. console.warn('defaultRegion非有效格式');
  49. }
  50. },
  51. immediate: true
  52. }
  53. },
  54. computed: {
  55. multiArray() {
  56. return this.pickedArr.map(arr => arr.map(item => item.name));
  57. },
  58. pickedArr() {
  59. // 进行初始化
  60. if (this.isInitMultiArray) {
  61. return [
  62. CHINA_REGIONS,
  63. CHINA_REGIONS[0].childs,
  64. CHINA_REGIONS[0].childs[0].childs
  65. ];
  66. }
  67. return [CHINA_REGIONS, this.cityArr, this.districtArr];
  68. }
  69. },
  70. methods: {
  71. handleColumnChange(e) {
  72. // console.log(e);
  73. this.isInitMultiArray = false;
  74. const that = this;
  75. let col = e.detail.column;
  76. let row = e.detail.value;
  77. that.multiIndex[col] = row;
  78. try {
  79. switch (col) {
  80. case 0:
  81. if (CHINA_REGIONS[that.multiIndex[0]].childs.length == 0) {
  82. that.cityArr = that.districtArr = [
  83. CHINA_REGIONS[that.multiIndex[0]]
  84. ];
  85. break;
  86. }
  87. that.cityArr = CHINA_REGIONS[that.multiIndex[0]].childs;
  88. that.districtArr =
  89. CHINA_REGIONS[that.multiIndex[0]].childs[
  90. that.multiIndex[1]
  91. ].childs;
  92. break;
  93. case 1:
  94. that.districtArr =
  95. CHINA_REGIONS[that.multiIndex[0]].childs[
  96. that.multiIndex[1]
  97. ].childs;
  98. break;
  99. case 2:
  100. break;
  101. }
  102. } catch (e) {
  103. // console.log(e);
  104. that.districtArr = CHINA_REGIONS[that.multiIndex[0]].childs[0].childs;
  105. }
  106. },
  107. handleValueChange(e) {
  108. // 结构赋值
  109. let [index0, index1, index2] = e.detail.value;
  110. let [arr0, arr1, arr2] = this.pickedArr;
  111. let address = [arr0[index0], arr1[index1], arr2[index2]];
  112. // console.log(address);
  113. this.$emit('getRegion', address);
  114. this.$emit(
  115. 'input',
  116. `${address[0].name}${address[1].name}${address[2].name}`
  117. );
  118. },
  119. handleDefaultRegion(region) {
  120. const isCode = !Array.isArray(region);
  121. this.isInitMultiArray = false;
  122. let children = CHINA_REGIONS;
  123. for (let i = 0; i < 3; i++) {
  124. for (let j = 0; j < children.length; j++) {
  125. let condition = isCode
  126. ? children[j].code == region.slice(0, (i + 1) * 2)
  127. : children[j].name.includes(region[i]);
  128. if (condition) {
  129. // 匹配成功进行赋值
  130. // console.log(i,j,children.length-1);
  131. children = children[j].childs;
  132. if (i == 0) {
  133. this.cityArr = children;
  134. } else if (i == 1) {
  135. this.districtArr = children;
  136. }
  137. this.$set(this.multiIndex, i, j);
  138. // console.log(this.multiIndex);
  139. break;
  140. } else {
  141. // 首次匹配失败就用默认的初始化
  142. // console.log(i,j,children.length-1);
  143. if (i == 0 && j == children.length - 1) {
  144. this.isInitMultiArray = true;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. };
  152. </script>