Winglau14-lotusAddress.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <!--地址picker-->
  3. <view :status="checkStatus" v-if="lotusAddressData.visible" class="lotus-address-mask">
  4. <view :class="lotusAddressData.visible?'lotus-address-box':'lotus-address-box lotus-address-box-out'">
  5. <view class="lotus-address-action">
  6. <text @tap="cancelPicker" class="lotus-address-action-cancel">取消</text>
  7. <text @tap="chosedVal" class="lotus-address-action-affirm">确认</text>
  8. </view>
  9. <view class="lotus-address-picker-box">
  10. <!--省-->
  11. <scroll-view scroll-y :scroll-into-view="'pid'+pChoseIndex" class="lotus-address-picker-box-item">
  12. <view @tap="clickPicker(0,pIndex,pItem);" :id="'pid'+pIndex" :class="pIndex === pChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(pItem,pIndex) in province" :key="pIndex">{{pItem}}</view>
  13. </scroll-view>
  14. <!--市-->
  15. <scroll-view scroll-y :scroll-into-view="'cid'+cChoseIndex" class="lotus-address-picker-box-item">
  16. <view @tap="clickPicker(1,cIndex,cItem);" :id="'cid'+cIndex" :class="cIndex === cChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(cItem,cIndex) in city" :key="cIndex">{{cItem}}</view>
  17. </scroll-view>
  18. <!--区-->
  19. <scroll-view scroll-y :scroll-into-view="'tid'+tChoseIndex" class="lotus-address-picker-box-item">
  20. <view @tap="clickPicker(2,tIndex,tItem);" :id="'tid'+tIndex" :class="tIndex === tChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(tItem,tIndex) in town" :key="tIndex">{{tItem}}</view>
  21. </scroll-view>
  22. <!--区END-->
  23. </view>
  24. </view>
  25. </view>
  26. <!--地址picker END-->
  27. </template>
  28. <script>
  29. import {lotusAddressJson} from "./Winglau14-lotusAddress.js";
  30. export default {
  31. props:['lotusAddressData'],
  32. data() {
  33. return {
  34. visible: false,
  35. province:[],
  36. city:[],
  37. town:[],
  38. provinceName:'',
  39. cityName:'',
  40. townName:'',
  41. type:0,//0新增1编辑
  42. pChoseIndex:-1,
  43. cChoseIndex:-1,
  44. tChoseIndex:-1
  45. };
  46. },
  47. methods:{
  48. //取消
  49. cancelPicker(){
  50. const provinceCode = this.getTarId(this.provinceName);
  51. const cityCode = this.getTarId(this.cityName);
  52. const townCode = this.getTarId(this.townName);
  53. this.visible = false;
  54. this.$emit("choseVal",{
  55. province:this.provinceName,
  56. provinceCode,
  57. city:this.cityName,
  58. cityCode,
  59. town:this.townName,
  60. townCode,
  61. isChose:0,
  62. visible:false
  63. });
  64. },
  65. //获取最后选择的省市区的值
  66. chosedVal() {
  67. this.type = 1;
  68. const provinceCode = this.getTarId(this.provinceName);
  69. const cityCode = this.getTarId(this.cityName);
  70. const townCode = this.getTarId(this.townName);
  71. this.visible = false;
  72. let isChose = 0;
  73. //已选省市区 isChose = 1
  74. if((this.provinceName&&this.cityName)||(this.provinceName&&this.cityName&&this.townName)){
  75. isChose = 1;
  76. }
  77. this.$emit("choseVal",{
  78. province:this.provinceName,
  79. provinceCode,
  80. city:this.cityName,
  81. cityCode,
  82. town:this.townName,
  83. townCode,
  84. isChose,
  85. visible:false
  86. });
  87. },
  88. //获取省市区value
  89. getTarId(name,type){
  90. let id = 0;
  91. lotusAddressJson.map((item,index)=>{
  92. if(item.name === name){
  93. id = item.value;
  94. }
  95. });
  96. return id;
  97. },
  98. //获取市数据
  99. getCityArr(parentId){
  100. let city = [];
  101. lotusAddressJson.map((item,index)=>{
  102. if(item.parent === parentId){
  103. city.push(item.name);
  104. }
  105. });
  106. return city;
  107. },
  108. //获取区数据
  109. getTownArr(parentId){
  110. let town = [];
  111. lotusAddressJson.map((item,index)=>{
  112. if(index>34&&item.parent === parentId){
  113. town.push(item.name);
  114. }
  115. });
  116. return town;
  117. },
  118. //初始化数据
  119. initFn(){
  120. if(!this.province.length){
  121. lotusAddressJson.map((item,index)=>{
  122. if(index<=34){
  123. this.province.push(item.name);
  124. }
  125. });
  126. }
  127. //已选择省市区,高亮显示对应选择省市区
  128. const p = this._props.lotusAddressData.provinceName;
  129. const c = this._props.lotusAddressData.cityName;
  130. const t = this._props.lotusAddressData.townName;
  131. //已选省
  132. if(p){
  133. this.pChoseIndex = this.getTarIndex(this.province,p);
  134. }
  135. //已选市
  136. if(p&&c){
  137. const pid = this.getTarId(p);
  138. this.city = this.getCityArr(pid);
  139. this.cChoseIndex = this.getTarIndex(this.city,c);
  140. }
  141. //已选区
  142. if(p&&c&&t){
  143. const cid= this.getTarId(c);
  144. this.town = this.getTownArr(cid);
  145. this.tChoseIndex = this.getTarIndex(this.town,t);
  146. }
  147. //未选省市区
  148. if(!p&&!c&&!t){
  149. this.pChoseIndex = -1;
  150. this.cChoseIndex = -1;
  151. this.tChoseIndex = -1;
  152. this.city = [];
  153. this.town = [];
  154. }
  155. },
  156. //获取已选省市区
  157. getChosedData(){
  158. const pid = this.getTarId(this.provinceName,'province');
  159. this.city = this.getCityArr(pid);
  160. const cid= this.getTarId(this.cityName,'city');
  161. this.town = this.getTownArr(cid);
  162. //已选省市区获取对应index
  163. if(this.provinceName){
  164. this.pChoseIndex = this.getTarIndex(this.province,this.provinceName);
  165. }
  166. if(this.cityName){
  167. this.cChoseIndex = this.getTarIndex(this.city,this.cityName);
  168. }
  169. if(this.townName){
  170. this.tChoseIndex = this.getTarIndex(this.town,this.townName);
  171. }
  172. },
  173. //选择省市区交互
  174. clickPicker(type,index,name){
  175. //省
  176. if(type === 0){
  177. this.pChoseIndex = index;
  178. this.provinceName = name;
  179. this.cChoseIndex = -1;
  180. this.tChoseIndex = -1;
  181. this.cityName = '';
  182. this.townName = '';
  183. }
  184. //市
  185. if(type ===1){
  186. this.cChoseIndex = index;
  187. this.cityName = name;
  188. this.tChoseIndex = -1;
  189. this.townName = '';
  190. }
  191. //区
  192. if(type === 2){
  193. this.tChoseIndex = index;
  194. this.townName = name;
  195. }
  196. //获取省市区数据
  197. this.getChosedData();
  198. },
  199. //获取已选省市区index
  200. getTarIndex(arr,tarName){
  201. let cIndex = 0;
  202. arr.map((item,index)=>{
  203. if(item === tarName){
  204. cIndex = index;
  205. }
  206. });
  207. return cIndex;
  208. }
  209. },
  210. computed:{
  211. checkStatus(){
  212. let t = null;
  213. const _this = this;
  214. if(!_this.visible){
  215. _this.visible = _this._props.lotusAddressData.visible;
  216. //获取省市区
  217. _this.provinceName = _this._props.lotusAddressData.provinceName;
  218. _this.cityName = _this._props.lotusAddressData.cityName;
  219. _this.townName = _this._props.lotusAddressData.townName;
  220. //生成初始化数据
  221. _this.initFn();
  222. t = _this.visible;
  223. }
  224. return t;
  225. }
  226. }
  227. }
  228. </script>
  229. <style lang="less">
  230. @import "./Winglau14-lotusAddress.css";
  231. </style>