vue實(shí)現(xiàn)手機(jī)端省市區(qū)區(qū)域選擇
本文實(shí)例為大家分享了vue實(shí)現(xiàn)手機(jī)端省市區(qū)區(qū)域選擇的具體代碼,供大家參考,具體內(nèi)容如下
1 后端接口獲取城市信息
2 先獲取省 根據(jù)用戶點(diǎn)擊的省獲取市
3 再根據(jù)用戶點(diǎn)擊的市獲取區(qū)
組件代碼:
<template>
<div class="city">
<!-- 點(diǎn)擊此處 省市區(qū)選擇出現(xiàn) -->
<div class="chooseCity" @click="clickCity">{{chooseCity}}</div>
<div class="boxcity" v-if="showCity">
<!-- 省市區(qū)的標(biāo)題 點(diǎn)擊可回退 -->
<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:"點(diǎn)擊我選擇",
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:{
//點(diǎn)擊省市標(biāo)題隱藏出現(xiàn)內(nèi)容 形成回退效果
chooseProvince(){
this.showProvince = true;
this.showCity2 = false;
},
chooseCity2(){
this.showProvince = false;
this.showCity2 = true;
this.showarea = false;
},
//點(diǎn)擊省市區(qū)出現(xiàn)
clickCity(){
this.showCity = true;
this.mackShow = true;
},
//點(diǎn)擊省市區(qū) 讓每個(gè)li內(nèi)展示的名字等于數(shù)據(jù)的城市名
getCity(){
for(var item of this.provinceL){
this.provinceName = item.regionName;
//this.regionId = item.regionId
}
},
//當(dāng)用戶點(diǎn)擊某個(gè)省事件 根據(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)用戶點(diǎn)擊某個(gè)市事件 根據(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;
})
},
//用戶點(diǎn)擊區(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;
}
},
//頁面初始化 請(qǐng)求數(shù)據(jù) 將請(qǐng)求到的城市保存下來
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%;
}
/* 讓滾動(dòng)條不顯示 */
.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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Electron-vue開發(fā)的客戶端支付收款工具的實(shí)現(xiàn)
這篇文章主要介紹了Electron-vue開發(fā)的客戶端支付收款工具的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
Vue實(shí)現(xiàn)生成本地Json文件功能方式
這篇文章主要介紹了Vue實(shí)現(xiàn)生成本地Json文件功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個(gè)人理解
這篇文章主要介紹了淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個(gè)人理解,v-model用于表單的數(shù)據(jù)綁定很常見,下面就來詳細(xì)的介紹一下2018-11-11
詳解vue表單驗(yàn)證組件 v-verify-plugin
本篇文章主要介紹了詳解vue表單驗(yàn)證組件 v-verify-plugin,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Vue.js實(shí)現(xiàn)圖片的隨意拖動(dòng)方法
下面小編就為大家分享一篇Vue.js實(shí)現(xiàn)圖片的隨意拖動(dòng)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue 動(dòng)態(tài)修改a標(biāo)簽的樣式的方法
這篇文章主要介紹了vue 動(dòng)態(tài)修改a標(biāo)簽的樣式的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
vxe-table?實(shí)現(xiàn)?excel?選擇一個(gè)單元格拖拽自動(dòng)復(fù)制新的單元格(示例代碼)
vxe-table是一款強(qiáng)大的表格組件,支持Excel風(fēng)格的操作,通過鼠標(biāo)右下角的擴(kuò)展按鈕,用戶可以拖拽選擇單元格并自動(dòng)復(fù)制內(nèi)容到擴(kuò)展區(qū)域的所有單元格中,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-01-01

