region-picker.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. import areaData from "./areadata/areadata.js"
  18. let _this=null;
  19. export default {
  20. data() {
  21. return {
  22. pickVal:[],
  23. range:{
  24. provinces:[],
  25. citys:[],
  26. areas:[]
  27. },
  28. checkObj:{}
  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.initData();
  52. }
  53. },
  54. created() {
  55. _this=this;
  56. _this.initData();
  57. },
  58. methods:{
  59. getData(){
  60. //用来处理初始化数据
  61. let provinces=areaData;
  62. let dVal=[];
  63. let value=this.value;
  64. let a1=value[0];//默认值省
  65. let a2=value[1];//默认值市
  66. let a3=value[2];//默认值区、县
  67. let province,city,area;
  68. let provinceIndex=provinces.findIndex((v)=>{
  69. return v[_this.defaultType]==a1
  70. });
  71. provinceIndex=value?(provinceIndex!=-1?provinceIndex:0):0;
  72. let citys=provinces[provinceIndex].children;
  73. let cityIndex=citys.findIndex((v)=>{
  74. return v[_this.defaultType]==a2
  75. });
  76. cityIndex=value?(cityIndex!=-1?cityIndex:0):0;
  77. let areas=citys[cityIndex].children;
  78. let areaIndex=areas.findIndex((v)=>{
  79. return v[_this.defaultType]==a3;
  80. });
  81. areaIndex=value?(areaIndex!=-1?areaIndex:0):0;
  82. dVal=_this.hideArea?[provinceIndex,cityIndex]:[provinceIndex,cityIndex,areaIndex];
  83. province=provinces[provinceIndex];
  84. city=citys[cityIndex];
  85. area=areas[areaIndex];
  86. let obj=_this.hideArea?{
  87. province,
  88. city
  89. }:{
  90. province,
  91. city,
  92. area
  93. }
  94. return _this.hideArea?{
  95. provinces,
  96. citys,
  97. dVal,
  98. obj
  99. }:{
  100. provinces,
  101. citys,
  102. areas,
  103. dVal,
  104. obj
  105. }
  106. },
  107. initData(){
  108. let dataData=_this.getData();
  109. let provinces=dataData.provinces;
  110. let citys=dataData.citys;
  111. let areas=_this.hideArea?[]:dataData.areas;
  112. let obj=dataData.obj;
  113. let province=obj.province,city=obj.city,area=_this.hideArea?{}:obj.area;
  114. let value=_this.hideArea?[province.value,city.value]:[province.value,city.value,area.value];
  115. let result=_this.hideArea?`${province.label+city.label}`:`${province.label+city.label+area.label}`;
  116. _this.range=_this.hideArea?{
  117. provinces,
  118. citys,
  119. }:{
  120. provinces,
  121. citys,
  122. areas
  123. };
  124. _this.checkObj=obj;
  125. _this.$nextTick(()=>{
  126. _this.pickVal=dataData.dVal;
  127. });
  128. _this.$emit("change",{
  129. result:result,
  130. value:value,
  131. obj:obj
  132. })
  133. },
  134. handlerChange(e){
  135. let arr=[...e.detail.value];
  136. let provinceIndex=arr[0],cityIndex=arr[1],areaIndex=_this.hideArea?0:arr[2];
  137. let provinces=areaData;
  138. let citys=(provinces[provinceIndex]&&provinces[provinceIndex].children)||provinces[provinces.length-1].children||[];
  139. let areas=_this.hideArea?[]:((citys[cityIndex]&&citys[cityIndex].children)||citys[citys.length-1].children||[]);
  140. let province=provinces[provinceIndex]||provinces[provinces.length-1],
  141. city=citys[cityIndex]||[citys.length-1],
  142. area=_this.hideArea?{}:(areas[areaIndex]||[areas.length-1]);
  143. let obj=_this.hideArea?{
  144. province,
  145. city
  146. }:{
  147. province,
  148. city,
  149. area
  150. }
  151. if(_this.checkObj.province.label!=province.label){
  152. //当省更新的时候需要刷新市、区县的数据;
  153. _this.range.citys=citys;
  154. if(!_this.hideArea){
  155. _this.range.areas=areas;
  156. }
  157. }
  158. if(_this.checkObj.city.label!=city.label){
  159. //当市更新的时候需要刷新区县的数据;
  160. if(!_this.hideArea){
  161. _this.range.areas=areas;
  162. }
  163. }
  164. _this.checkObj=obj;
  165. _this.$nextTick(()=>{
  166. _this.pickVal=arr;
  167. })
  168. let result=_this.hideArea?`${province.label+city.label}`:`${province.label+city.label+area.label}`;
  169. let value=_this.hideArea?[province.value,city.value]:[province.value,city.value,area.value];
  170. _this.$emit("change",{
  171. result:result,
  172. value:value,
  173. obj:obj
  174. })
  175. }
  176. }
  177. }
  178. </script>
  179. <style lang="scss">
  180. @import "./w-picker.css";
  181. </style>