RegionSel.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <picker mode="multiSelector" @change="pickerRegionChange" @columnchange="columnRegionChange" :value="Rindex" :range="region" range-key="label"><slot></slot></picker>
  3. </template>
  4. <script>
  5. import RegionJson from './area.json';
  6. export default {
  7. props: {
  8. provinceCode: {
  9. type: [String, Number],
  10. default: () => {
  11. return 0;
  12. }
  13. },
  14. cityCode: {
  15. type: [String, Number],
  16. default: () => {
  17. return 0;
  18. }
  19. },
  20. districtCode: {
  21. type: [String, Number],
  22. default: () => {
  23. return 0;
  24. }
  25. }
  26. },
  27. watch: {
  28. districtCode() {
  29. this.getRegionName();
  30. }
  31. },
  32. data() {
  33. return {
  34. Rindex: [0, 0, 0],
  35. region: [],
  36. province_code: '',
  37. city_code: ''
  38. };
  39. },
  40. async created() {
  41. // await this.getProvince()
  42. // await this.getCity()
  43. // await this.getDistrict()
  44. let region = JSON.parse(JSON.stringify(this.region));
  45. // data.unshift({code:0,id:0,name:'全国'})
  46. region[0] = RegionJson;
  47. region[1] = RegionJson[0].children;
  48. region[2] = RegionJson[0].children[0].children;
  49. // this.province_code = RegionJson[0].value
  50. this.region = region;
  51. await this.getRegionName();
  52. },
  53. methods: {
  54. // 获取名字
  55. async getRegionName() {
  56. if (!this.provinceCode) return;
  57. const provinceIndex = RegionJson.findIndex(item => item.value === parseInt(this.provinceCode));
  58. const province = RegionJson.find(item => item.value === parseInt(this.provinceCode));
  59. const cityIndex = province.children.findIndex(item => item.value === parseInt(this.cityCode));
  60. const city = province.children.find(item => item.value === parseInt(this.cityCode));
  61. const districtIndex = city.children.findIndex(item => item.value === parseInt(this.districtCode));
  62. const district = city.children.find(item => item.value === parseInt(this.districtCode));
  63. const regionName = province.label + '-' + city.label + '-' + district.label;
  64. this.region = [RegionJson, province.children, city.children];
  65. this.Rindex = [provinceIndex, cityIndex, districtIndex];
  66. this.$emit('getRegionName', regionName);
  67. },
  68. // 省
  69. getProvince() {
  70. this.$u.api.getAllProvince().then(({data})=>{
  71. let region = JSON.parse(JSON.stringify(this.region));
  72. // data.unshift({code:0,id:0,name:'全国'})
  73. region[0] = data;
  74. this.province_code = data[0].code;
  75. this.region = region;
  76. });
  77. },
  78. // 市
  79. async getCity() {
  80. if (!this.province_code) {
  81. let region = JSON.parse(JSON.stringify(this.region));
  82. region[1] = [];
  83. this.region = region;
  84. return;
  85. }
  86. this.$u.api.getAllCityByProvinceCode(this.province_code).then(({data})=>{
  87. let region = JSON.parse(JSON.stringify(this.region));
  88. region[1] = data;
  89. this.city_code = data[0].code;
  90. this.region = region;
  91. });
  92. },
  93. // 区
  94. async getDistrict() {
  95. this.$u.api.getAllAreaByCityCode(this.city_code).then(({data})=>{
  96. let region = JSON.parse(JSON.stringify(this.region));
  97. region[2] = data;
  98. this.region = region;
  99. });
  100. },
  101. // select选择器 确定
  102. pickerRegionChange(e) {
  103. const i = e.detail.value;
  104. this.Rindex = i;
  105. const params = [
  106. this.region[0][i[0]],
  107. this.region[1] && this.region[1].length > 0 ? this.region[1][i[1]] : '',
  108. this.region[2] && this.region[2].length > 0 ? this.region[2][i[2]] : ''
  109. ];
  110. this.$emit('pickerRegionChange', params);
  111. },
  112. //某一列的值改变时
  113. async columnRegionChange(e) {
  114. const column = e.detail.column;
  115. const i = e.detail.value;
  116. if (column === 0) {
  117. let region = JSON.parse(JSON.stringify(this.region));
  118. region[1] = RegionJson[i].children;
  119. if(region[1].length){
  120. region[2] = RegionJson[i].children[0].children;
  121. }else{
  122. region[2] = []
  123. }
  124. this.region = region;
  125. }
  126. if (column === 1) {
  127. let region = JSON.parse(JSON.stringify(this.region));
  128. region[2] = region[1][i].children;
  129. this.region = region;
  130. }
  131. }
  132. }
  133. };
  134. </script>
  135. <style></style>