Winglau14-lotusAddress.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <!--地址picker-->
  3. <view :status="checkStatus" v-if="lotusAddressData.visible" class="lotus-address-mask">
  4. <view class="lotus-address-box">
  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.$emit("choseVal",{
  54. provice:this.provinceName,
  55. provinceCode,
  56. city:this.cityName,
  57. cityCode,
  58. town:this.townName,
  59. townCode,
  60. isChose:0,
  61. visible:false
  62. });
  63. },
  64. //获取最后选择的省市区的值
  65. chosedVal() {
  66. this.type = 1;
  67. const provinceCode = this.getTarId(this.provinceName);
  68. const cityCode = this.getTarId(this.cityName);
  69. const townCode = this.getTarId(this.townName);
  70. this.$emit("choseVal",{
  71. provice:this.provinceName,
  72. provinceCode,
  73. city:this.cityName,
  74. cityCode,
  75. town:this.townName,
  76. townCode,
  77. isChose:1,
  78. visible:false
  79. });
  80. },
  81. //获取省市区value
  82. getTarId(name,type){
  83. let id = 0;
  84. const _this = this;
  85. lotusAddressJson.map((item,index)=>{
  86. if(item.name === name){
  87. id = item.value;
  88. }
  89. });
  90. return id;
  91. },
  92. //获取市数据
  93. getCityArr(parentId){
  94. let city = [];
  95. lotusAddressJson.map((item,index)=>{
  96. if(item.parent === parentId){
  97. city.push(item.name);
  98. }
  99. });
  100. return city;
  101. },
  102. //获取区数据
  103. getTownArr(parentId){
  104. let town = [];
  105. lotusAddressJson.map((item,index)=>{
  106. if(index>34&&item.parent === parentId){
  107. town.push(item.name);
  108. }
  109. });
  110. return town;
  111. },
  112. //初始化数据
  113. initFn(){
  114. console.log(1);
  115. lotusAddressJson.map((item,index)=>{
  116. if(index<=34){
  117. this.province.push(item.name);
  118. }
  119. });
  120. //已选择省市区,高亮显示对应选择省市区
  121. const p = this._props.lotusAddressData.provinceName;
  122. const c = this._props.lotusAddressData.cityName;
  123. const t = this._props.lotusAddressData.townName;
  124. if(p){
  125. this.pChoseIndex = this.getTarIndex(this.province,p);
  126. }
  127. if(p&&c){
  128. const pid = this.getTarId(p);
  129. this.city = this.getCityArr(pid);
  130. this.cChoseIndex = this.getTarIndex(this.city,c);
  131. }
  132. if(p&&c&&t){
  133. const cid= this.getTarId(c);
  134. this.town = this.getTownArr(cid);
  135. this.tChoseIndex = this.getTarIndex(this.town,t);
  136. }
  137. },
  138. //获取已选省市区
  139. getChosedData(){
  140. const pid = this.getTarId(this.provinceName,'provice');
  141. this.city = this.getCityArr(pid);
  142. const cid= this.getTarId(this.cityName,'city');
  143. this.town = this.getTownArr(cid);
  144. //已选省市区获取对应index
  145. if(this.provinceName){
  146. this.pChoseIndex = this.getTarIndex(this.province,this.provinceName);
  147. }
  148. if(this.cityName){
  149. this.cChoseIndex = this.getTarIndex(this.city,this.cityName);
  150. }
  151. if(this.townName){
  152. this.tChoseIndex = this.getTarIndex(this.town,this.townName);
  153. }
  154. },
  155. //选择省市区交互
  156. clickPicker(type,index,name){
  157. //省
  158. if(type === 0){
  159. this.pChoseIndex = index;
  160. this.provinceName = name;
  161. this.cChoseIndex = -1;
  162. this.tChoseIndex = -1;
  163. this.cityName = '';
  164. this.townName = '';
  165. }
  166. //市
  167. if(type ===1){
  168. this.cChoseIndex = index;
  169. this.cityName = name;
  170. this.tChoseIndex = -1;
  171. this.townName = '';
  172. }
  173. //区
  174. if(type === 2){
  175. this.tChoseIndex = index;
  176. this.townName = name;
  177. }
  178. //获取省市区数据
  179. this.getChosedData();
  180. },
  181. //获取已选省市区index
  182. getTarIndex(arr,tarName){
  183. let cIndex = 0;
  184. arr.map((item,index)=>{
  185. if(item === tarName){
  186. cIndex = index;
  187. }
  188. });
  189. return cIndex;
  190. }
  191. },
  192. created() {
  193. this.provinceName = this._props.lotusAddressData.provinceName;
  194. this.cityName = this._props.lotusAddressData.cityName;
  195. this.townName = this._props.lotusAddressData.townName;
  196. },
  197. computed:{
  198. checkStatus(){
  199. let t = null;
  200. const _this = this;
  201. if(!_this.visible){
  202. _this.initFn();
  203. _this.visible = _this._props.lotusAddressData.visible;
  204. t = _this.visible;
  205. }
  206. return t;
  207. }
  208. }
  209. }
  210. </script>
  211. <style lang="less">
  212. @import "./Winglau14-lotusAddress.css";
  213. </style>