pick-regions.vue 5.1 KB

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