region-picker.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <view class="w-picker-view">
  3. <picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  4. <picker-view-column>
  5. <view class="w-picker-item" v-for="(item,index) in range.provinces" :key="index">{{item.label}}</view>
  6. </picker-view-column>
  7. <picker-view-column>
  8. <view class="w-picker-item" v-for="(item,index) in range.citys" :key="index">{{item.label}}</view>
  9. </picker-view-column>
  10. <picker-view-column v-if="!hideArea">
  11. <view class="w-picker-item" v-for="(item,index) in range.areas" :key="index">{{item.label}}</view>
  12. </picker-view-column>
  13. </picker-view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. pickVal:[],
  21. range:{
  22. provinces:[],
  23. citys:[],
  24. areas:[]
  25. },
  26. checkObj:{},
  27. dataData:[],
  28. areaData:[]
  29. };
  30. },
  31. props:{
  32. itemHeight:{
  33. type:String,
  34. default:"44px"
  35. },
  36. value:{
  37. type:[Array,String],
  38. default:""
  39. },
  40. defaultType:{
  41. type:String,
  42. default:"label"
  43. },
  44. hideArea:{
  45. type:Boolean,
  46. default:false
  47. }
  48. },
  49. watch:{
  50. value(val){
  51. this.getData();
  52. }
  53. },
  54. created() {
  55. this.getData();
  56. },
  57. methods:{
  58. getData(){
  59. this.$http.area_tree().then(res => {
  60. if(res.data.code===1){
  61. console.log(res.data.data)
  62. //用来处理初始化数据
  63. this.areaData=res.data.data
  64. let provinces=res.data.data;
  65. let dVal=[];
  66. let value=this.value;
  67. let a1=value[0];//默认值省
  68. let a2=value[1];//默认值市
  69. let a3=value[2];//默认值区、县
  70. let province,city,area;
  71. let provinceIndex=provinces.findIndex((v)=>{
  72. return v[this.defaultType]==a1
  73. });
  74. provinceIndex=value?(provinceIndex!=-1?provinceIndex:0):0;
  75. let citys=provinces[provinceIndex].children;
  76. let cityIndex=citys.findIndex((v)=>{
  77. return v[this.defaultType]==a2
  78. });
  79. cityIndex=value?(cityIndex!=-1?cityIndex:0):0;
  80. let areas=citys[cityIndex].children;
  81. let areaIndex=areas.findIndex((v)=>{
  82. return v[this.defaultType]==a3;
  83. });
  84. areaIndex=value?(areaIndex!=-1?areaIndex:0):0;
  85. dVal=this.hideArea?[provinceIndex,cityIndex]:[provinceIndex,cityIndex,areaIndex];
  86. province=provinces[provinceIndex];
  87. city=citys[cityIndex];
  88. area=areas[areaIndex];
  89. let obj=this.hideArea?{
  90. province,
  91. city
  92. }:{
  93. province,
  94. city,
  95. area
  96. }
  97. this.dataData = this.hideArea?{
  98. provinces,
  99. citys,
  100. dVal,
  101. obj
  102. }:{
  103. provinces,
  104. citys,
  105. areas,
  106. dVal,
  107. obj
  108. },
  109. this.initData()
  110. }
  111. });
  112. },
  113. initData(){
  114. let dataData=this.dataData;
  115. let provinces=dataData.provinces;
  116. let citys=dataData.citys;
  117. let areas=this.hideArea?[]:dataData.areas;
  118. let obj=dataData.obj;
  119. if(!obj.province){
  120. return;
  121. }
  122. let province=obj.province,city=obj.city,area=this.hideArea?{}:obj.area;
  123. let value=this.hideArea?[province.value,city.value]:[province.value,city.value,area.value];
  124. let result=this.hideArea?`${province.label+city.label}`:`${province.label+city.label+area.label}`;
  125. this.range=this.hideArea?{
  126. provinces,
  127. citys,
  128. }:{
  129. provinces,
  130. citys,
  131. areas
  132. };
  133. this.checkObj=obj;
  134. this.$nextTick(()=>{
  135. this.pickVal=dataData.dVal;
  136. });
  137. this.$emit("change",{
  138. result:result,
  139. value:value,
  140. obj:obj
  141. })
  142. },
  143. handlerChange(e){
  144. let arr=[...e.detail.value];
  145. let provinceIndex=arr[0],cityIndex=arr[1],areaIndex=this.hideArea?0:arr[2];
  146. let provinces=this.areaData;
  147. let citys=(provinces[provinceIndex]&&provinces[provinceIndex].children)||provinces[provinces.length-1].children||[];
  148. let areas=this.hideArea?[]:((citys[cityIndex]&&citys[cityIndex].children)||citys[citys.length-1].children||[]);
  149. let province=provinces[provinceIndex]||provinces[provinces.length-1],
  150. city=citys[cityIndex]||[citys.length-1],
  151. area=this.hideArea?{}:(areas[areaIndex]||[areas.length-1]);
  152. let obj=this.hideArea?{
  153. province,
  154. city
  155. }:{
  156. province,
  157. city,
  158. area
  159. }
  160. if(this.checkObj.province.label!=province.label){
  161. //当省更新的时候需要刷新市、区县的数据;
  162. this.range.citys=citys;
  163. if(!this.hideArea){
  164. this.range.areas=areas;
  165. }
  166. }
  167. if(this.checkObj.city.label!=city.label){
  168. //当市更新的时候需要刷新区县的数据;
  169. if(!this.hideArea){
  170. this.range.areas=areas;
  171. }
  172. }
  173. this.checkObj=obj;
  174. this.$nextTick(()=>{
  175. this.pickVal=arr;
  176. })
  177. let result=this.hideArea?`${province.label+city.label}`:`${province.label+city.label+area.label}`;
  178. let value=this.hideArea?[province.value,city.value]:[province.value,city.value,area.value];
  179. this.$emit("change",{
  180. result:result,
  181. value:value,
  182. obj:obj
  183. })
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="scss">
  189. @import "./w-picker.css";
  190. </style>