vue實現(xiàn)手機端省市區(qū)區(qū)域選擇
更新時間:2021年10月21日 15:55:48 作者:小羽向前跑
這篇文章主要為大家詳細介紹了vue實現(xiàn)手機端省市區(qū)區(qū)域選擇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)手機端省市區(qū)區(qū)域選擇的具體代碼,供大家參考,具體內(nèi)容如下
1 后端接口獲取城市信息
2 先獲取省 根據(jù)用戶點擊的省獲取市
3 再根據(jù)用戶點擊的市獲取區(qū)
組件代碼:
<template>
<div class="city">
<!-- 點擊此處 省市區(qū)選擇出現(xiàn) -->
<div class="chooseCity" @click="clickCity">{{chooseCity}}</div>
<div class="boxcity" v-if="showCity">
<!-- 省市區(qū)的標題 點擊可回退 -->
<div class="chooseTit">
<p @click="chooseProvince" v-show='tit1'>{{chooseTit1}}</p>
<p @click="chooseCity2" v-show='tit2'>{{chooseTit2}}</p>
<p v-show='tit3'>{{chooseTit3}}</p>
</div>
<!-- 省市區(qū) -->
<div class="citys">
<div @click="getCity" class="province">
<ul v-show="showProvince">
<li v-for="item in provinceL" :key="item.regionId" @click="getProvince(item)">{{item.regionName}}</li>
</ul>
<ul v-show="showCity2">
<li v-for="item in cityL" :key="item.regionId" @click="getCity2(item)">{{item.regionName}}</li>
</ul>
<ul v-show="showarea">
<li v-for="item in areaL" :key="item.regionId" @click="getarea(item)">{{item.regionName}}</li>
</ul>
</div>
</div>
</div>
<!-- 遮罩層 -->
<div class="mask" v-show="mackShow" @click="closeMask"></div>
</div>
</template>
<script>
export default {
data () {
return {
chooseCity:"點擊我選擇",
selected : '',
citySelected: '',
areaSelected: '',
provinceL : [],
cityL : [],
areaL : [],
city : [],
provinceName: '',
cityName : '',
areaName : '',
showProvince:true,
showCity:false,
showCity2:false,
showarea:false,
chooseTit1:"省",
chooseTit2:"市",
chooseTit3:"區(qū)",
tit1:true,
tit2:false,
tit3:false,
mackShow:false,
province:"",
Nextcity:"",
district:"",
totalCity:"",
}
},
methods:{
//點擊省市標題隱藏出現(xiàn)內(nèi)容 形成回退效果
chooseProvince(){
this.showProvince = true;
this.showCity2 = false;
},
chooseCity2(){
this.showProvince = false;
this.showCity2 = true;
this.showarea = false;
},
//點擊省市區(qū)出現(xiàn)
clickCity(){
this.showCity = true;
this.mackShow = true;
},
//點擊省市區(qū) 讓每個li內(nèi)展示的名字等于數(shù)據(jù)的城市名
getCity(){
for(var item of this.provinceL){
this.provinceName = item.regionName;
//this.regionId = item.regionId
}
},
//當(dāng)用戶點擊某個省事件 根據(jù)省的id獲取市
getProvince(item){
this.province = item.regionName
console.log(this.province);
// console.log(item.regionId);
this.$axios({
url:'http://192.168.1.16:0000/insurance-intact-wechat-api/get_regions?parentId='+item.regionId,
method: 'get'
}).then(res=>{
//console.log(res)
this.cityL = res.data;
this.citySelected = this.cityL[0].regionId;
this.showProvince = false;
this.showCity2= true;
this.tit2 = true;
})
this.areaL = [];
},
//當(dāng)用戶點擊某個市事件 根據(jù)省的id獲取區(qū)
getCity2(item){
this.Nextcity = item.regionName
console.log(this.Nextcity);
// console.log(item.regionId);
this.$axios({
url:'http://192.168.1.16:0000/insurance-intact-wechat-api/get_regions?parentId='+item.regionId,
method: 'get'
}).then(res=>{
//console.log(res)
this.areaL = res.data;
this.areaSelected = this.areaL[0].regionId;
this.showarea = true;
this.showCity2= false;
this.tit3 = true;
})
},
//用戶點擊區(qū)或者鎮(zhèn),遮罩消失
getarea(item){
this.district = item.regionName;
console.log(this.district);
var totalCity = this.province+"," + this.Nextcity +"," +this.district;
this.chooseCity =totalCity;
//console.log(item.regionId);
this.showCity = false;
this.mackShow = false;
},
closeMask(){
this.showCity = false;
this.mackShow = false;
}
},
//頁面初始化 請求數(shù)據(jù) 將請求到的城市保存下來
created() {
var url="http://192.168.1.16:0000/insurance-intact-wechat-api/get_regions?parentId=0";
this.$axios({
method:'get',
url:url,
withCredentials: true,
crossDomain: true,
data:"data",
headers: {
'Content-Type':'application/x-www-form-urlencoded',
}
}).then(res=>{
//console.log(res.data);
this.provinceL = res.data;
})
.catch(error=>{
console.log(error);
});
},
}
</script>
<style scoped>
.chooseCity{
width: 100%;
height: 40px;
text-align: center;
line-height: 40px;
border-bottom: 1px solid #666;
}
.boxcity{
position: absolute;
width: 40%;
right: 0;
top:0;
height: 60%;
z-index: 100;
background: #ffffff;
}
.citys{
border-top: 1px solid #666;
height: 100%;
overflow: hidden;
overflow-y: scroll;
background: #ffffff;
}
.province{
height: 100%;
}
/* 讓滾動條不顯示 */
.citys::-webkit-scrollbar {
display: none;
}
ul{
margin:0;
padding:0;
}
li{
list-style: none;
margin-top: 10px;
}
.chooseTit {
display: flex;
justify-content: space-around;
width: 100%;
text-align: center;
background: #448ff7;
}
.chooseTit p{
color: #ffffff;
}
.mask{
position: absolute;
width: 100%;
height: 100%;
background: black;
opacity: .5;
top:0;
left: 0;
z-index: 90;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Electron-vue開發(fā)的客戶端支付收款工具的實現(xiàn)
這篇文章主要介紹了Electron-vue開發(fā)的客戶端支付收款工具的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解
這篇文章主要介紹了淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解,v-model用于表單的數(shù)據(jù)綁定很常見,下面就來詳細的介紹一下2018-11-11
vxe-table?實現(xiàn)?excel?選擇一個單元格拖拽自動復(fù)制新的單元格(示例代碼)
vxe-table是一款強大的表格組件,支持Excel風(fēng)格的操作,通過鼠標右下角的擴展按鈕,用戶可以拖拽選擇單元格并自動復(fù)制內(nèi)容到擴展區(qū)域的所有單元格中,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2025-01-01

