仿照小米之家做的一个省市区三级联动,先上代码:

HTML:

<template>
<section class="myAddress">
<section>
<section class="cont" @click="choseAdd()">
<section>
<span>所在地区:{{Province?Province:''}} {{City && City!== '请选择'?City:''}} {{District && District !== '请选择'?District:''}}</span>
</section>
<div style="clear: both"></div>
</section>
</section>
<!-- 省市区三级联动选项 -->
<section class="showChose" v-show="showChose">
<section class="address">
<section class="title">
<h4>选择您所在的地区</h4>
<span @click="closeAdd()">×</span>
</section>
<section class="title">
<div class="area" @click="provinceSelected(0)" :class="tabIndex===0?'active':''">
{{Province?Province:'请选择'}}
</div>
<div class="area" @click="citySelected(1)" :class="tabIndex===1?'active':''" v-show="Province">
{{City?City:'请选择'}}
</div>
<div class="area" @click="districtSelected(2)" :class="tabIndex===2?'active':''" v-show="City && hasDistrict">
{{District?District:'请选择'}}
</div>
</section>
<ul>
<!-- 常用城市 -->
<div v-show="showProvince" class="frequentCity">
<p class="frequentCityTip" v-show="showFrequentCity">常用城市</p>
<div class="frequentCityList">
<span class="cityName" v-for="(frequentCity, index) in frequentCitys" :key="'frequentCity'+index" @click="selectFrequentCity(frequentCity)">广州</span>
</div>
</div>
<!-- 省市区列表 -->
<li class="addList" v-for="(v , k) in info" @click="getProvinceId(v.index, v.AREA_NAME, k)" v-show="showProvince" :class="v.selected ? 'active' : ''">{{v.AREA_NAME}}</li>
<li class="addList" v-for="(v,k) in showCityList" @click="getCityId(v.index, v.AREA_NAME, k)" v-show="showCity" :class="v.selected ? 'active' : ''">{{v.AREA_NAME}}</li>
<li class="addList" v-for="(v,k) in showDistrictList" @click="getDistrictId(v.index, v.AREA_NAME, k)" v-show="showDistrict" :class="v.selected ? 'active' : ''">{{v.AREA_NAME}}</li>
</ul>
</section>
</section>
</section>
</template>

JS:

export default {
name: 'address',
data () {
return {
showChose: false, // 是否显示省市区弹框
showProvince: true, // 显示省份列表
showCity: false, // 显示城市列表
showDistrict: false, // 显示区列表
showCityList: false, // 城市数据列表
showDistrictList: false, // 区数据列表
province: , // 当前选择的省份index
city: , // 当前选择的城市index
district: , // 当前选择的区index
District: false, // 区名字
Province: false, // 省名字
City: false, // 城市名字
areaProvince: '',
areaCity: '',
areaDistrict: '',
tabIndex: , // 当前选择的tab下标
hasDistrict: true, // 是否有区
selected: false, // 是否选中(active)
info: [], // 后台交互的省市区接口数据
frequentCitys: [], // 常用城市数据
saveCityData: [], // 存储选择的省市区的缓存数据
showFrequentCity: false // 是否显示常用城市
}
},
components: {
},
computed: {
},
created () {
// 获取省市区数据
this.getAreaData()
// 从缓存读取常用城市数据
this.frequentCitys = JSON.parse(localStorage.getItem('frequentList'))
this.saveCityData = JSON.parse(localStorage.getItem('frequentList'))
if (!this.frequentCitys) {
this.showFrequentCity = false
} else {
this.showFrequentCity = true
}
},
mounted () {
},
methods: {
// 获取省市区数据
getAreaData () {
console.log('获取省市区数据')
this.$root._axios('post', 'url', {})
.then(res => {
console.log('res', res.data.nodes)
if (res.data.nodes.length <= ) {
console.log('网络异常')
} else {
this.info = res.data.nodes
}
})
},
// 选择常用城市
selectFrequentCity: function (frequentCityData) {
console.log('frequentCityData', frequentCityData)
},
// 点击选择省市区
choseAdd: function () {
this.showChose = true
},
// 关闭弹框
closeAdd: function () {
this.showChose = false
},
/* eslint-disable */
// 对选择当前的数据,进行下一级的数据的筛选
_filter (add, name, code) {
let result = []
for (let i = ; i < add.length; i++) {
if (code == add[i].index) {
result = add[i][name]
}
}
return result
},
/* eslint-enable */
// 选择省份列表
getProvinceId: function (code, input, index) {
this.tabIndex =
this.province = code
this.Province = input
this.showProvince = false
this.showCity = true
this.showDistrict = false
if (!this.City) {
} else {
this.City = '请选择'
}
if (!this.District) {
} else {
this.hasDistrict = false
this.District = '请选择'
}
this.showCityList = this._filter(this.info, 'city', this.province)
// 点击选择当前
/* eslint-disable */
this.info.map(a => a.selected = false)
/* eslint-enable */
this.info[index].selected = true
this.areaProvince = input
},
// 点击省份tab
provinceSelected: function (index) {
this.tabIndex = index
// 选项页面的切换
this.showProvince = true
this.showCity = false
this.showDistrict = false
},
// 选择城市列表
getCityId: function (code, input, index) {
this.tabIndex =
this.city = code
this.City = input
this.showProvince = false
this.showCity = false
this.showDistrict = true
this.District = '请选择'
this.showDistrictList = this._filter(this.showCityList, 'district', this.city)
console.log('this.showDistrictList', this.showDistrictList)
// 选择当前添加active
/* eslint-disable */
this.showCityList.map(a => a.selected = false)
/* eslint-enable */
this.showCityList[index].selected = true
this.areaCity = input
// 判断当前选的城市是否有地区
if (this.showDistrictList.length === ) {
this.hasDistrict = false
this.showDistrict = false
this.District = false
this.showChose = false
// 把选择的省市放入缓存中
let selectCity = {}
selectCity.province = this.Province
selectCity.city = this.City
selectCity.district = ''
this.saveCityData.push(selectCity)
localStorage.setItem('frequentList', JSON.stringify(this.saveCityData))
} else {
this.hasDistrict = true
this.showDistrict = true
}
},
// 点击城市tab
citySelected: function (index) {
this.tabIndex = index
this.showProvince = false
this.showCity = true
this.showDistrict = false
},
// 选择区列表
getDistrictId: function (code, input, index) {
this.district = code
this.District = input
// 选择当前添加active
/* eslint-disable */
this.showDistrictList.map(a => a.selected = false)
/* eslint-enable */
this.showDistrictList[index].selected = true
// 选取市区选项之后关闭弹层
this.showChose = false
this.areaDistrict = input
// 把选择的数据放入缓存中
let selectCity = {}
selectCity.province = this.Province
selectCity.city = this.City
selectCity.district = this.District
this.saveCityData.push(selectCity)
localStorage.setItem('frequentList', JSON.stringify(this.saveCityData))
},
// 点击区tab
districtSelected: function (index) {
this.tabIndex = index
this.showProvince = false
this.showCity = false
this.showDistrict = true
}
}
}

CSS:

.myAddress {
width: %;
background-color: white;
border-top: 4px solid rgba(, , , );
color: #;
}
.myAddress .cont {
border-bottom: 1px solid rgba(, , , 0.8);
}
.myAddress .cont span {
display: inline-block;
font-size: .28rem;
color: #;
line-height: .88rem;
margin-left: .32rem;
}
.myAddress .cont section {
float: left;
}
.myAddress .cont img {
float: right;
width: .14rem;
height: .24rem;
margin: .32rem .32rem .32rem ;
}
.showChose {
width: %;
height: %;
position: fixed;
top: ;
left: ;
z-index: ;
background: rgba(, , , 0.8);
}
.address {
position: absolute;
bottom: ;
left: ;
z-index: ;
background: #fff;
width: %;
}
.title h4 {
display: inline-block;
margin-left: 2rem;
font-size: .32rem;
line-height: .88rem;
font-weight: normal;
color: #;
}
.title span {
margin: .42rem .2rem;
font-size: .45rem;
line-height: .34rem;
color: #D8D8D8;
}
.area {
display: inline-block;
font-size: .24rem;
line-height: .88rem;
margin-left: .42rem;
color: #;
}
.addList {
padding-left: .32rem;
font-size: .34rem;
line-height: .88rem;
color: #;
}
/* 修改的格式 */
.address ul {
height: .4rem;
margin-left: %;
max-height: .4rem;
overflow: auto;
}
.address .title .active {
color: #0071B8;
border-bottom: .02rem solid #0071B8;
}
.address ul .active {
color: #0071B8;
}
.frequentCity{
width: %;
}
.frequentCityTip{
text-align: left;font-size: .3rem;margin: .3rem;font-weight: bold;
}
.frequentCityList{
display: -webkit-box; /* Chrome 4+, Safari 3.1, iOS Safari 3.2+ */
display: -moz-box; /* Firefox 17- */
display: -webkit-flex; /* Chrome 21+, Safari 6.1+, iOS Safari 7+, Opera 15/16 */
display: -moz-flex; /* Firefox 18+ */
display: -ms-flexbox; /* IE 10 */
display: flex; /* Chrome 29+, Firefox 22+, IE 11+, Opera 12.1/17/18, Android 4.4+ */
flex-wrap: wrap;margin-right: %;margin-left: %;
}
.cityName{
letter-spacing: .06rem;margin: .1rem .1rem .3rem .6rem;font-size: .29rem;
}

逻辑分析:首先调用接口,获取省市区数据,然后对省市区数据进行拆分。

具体分析,后面更新

这个是调用接口返回的数据其中一些,具体的参数还可以根据需求再添加。

info: [
{
index: ,
AREA_NAME: '北京',
city: [
{
index: ,
AREA_NAME: '北京市',
district: [
{
index: ,
AREA_NAME: '东城区'
},
{
index: ,
AREA_NAME: '西城区'
},
{
index: ,
AREA_NAME: '崇文区'
}
]
}
]
},
{
index: ,
AREA_NAME: '河北',
city: [
{
index: ,
AREA_NAME: '石家庄市',
district: [
{
index: ,
AREA_NAME: '长安区'
},
{
index: ,
AREA_NAME: '桥东区'
},
{
index: ,
AREA_NAME: '桥西区'
}
]
},
{
index: ,
AREA_NAME: '唐山市',
district: [
{
index: ,
AREA_NAME: '路南区'
},
{
index: ,
AREA_NAME: '路北区'
},
{
index: ,
AREA_NAME: '古冶区'
}
]
}
]

vue省市区三级联动的更多相关文章

  1. vue省市区三级联动(高仿京东)

    该栗子是我直接从公司的项目单独拉出来的(懒得重新写一次了),所以代码会有些冗余,下面直接看效果: 接着上代码: html: <template> <div> <div c ...

  2. vue仿京东省市区三级联动选择组件

    工作中需要一个盒京东购物车地址选择相似的一个省市区三级联动选择组件,google查了下都是下拉框形式的,于是自己写了一个,希望对使用vue开发项目的朋友有帮助,显示效果如下:使用vue2.0开发 ht ...

  3. vue 引用省市区三级联动(element-ui Cascader)

    npm 下载 npm install element-china-area-data -S main.js import {provinceAndCityData,regionData,provinc ...

  4. vue 引用省市区三级联动(插件)

    vue 用省市区三级联动之傻瓜式教程(复制粘贴即用) npm 下载 npm install v-distpicker --save main.js //引入 省市区三级联动 import Distpi ...

  5. JS实现年月日三级联动+省市区三级联动+国家省市三级联动

    开篇随笔:最近项目需要用到关于年月日三级联动以及省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例或者只有单纯的js还不完整,却很难找到详细的具体数据(baidu搜索都 ...

  6. jQuery省市区三级联动插件

    体验效果:http://hovertree.com/texiao/bootstrap/4/支持PC和手机移动端. 手机扫描二维码体验效果: 代码如下: <!DOCTYPE html> &l ...

  7. 省市区三级联动 pickerView

    效果图 概述 关于 省市区 三级联动的 pickerView,我想大多数的 iOS 开发者应该都遇到过这样的需求.在遇到这样的需求的时候,大多数人都会觉的这个很复杂,一时无从下手.其实真的没那么复杂. ...

  8. JS省市区三级联动

    不需要访问后台服务器端,不使用Ajax,无刷新,纯JS实现的省市区三级联动. 当省市区数据变动是只需调正js即可. 使用方法: <!DOCTYPE html><html>< ...

  9. ajax省市区三级联动

    jdbc+servlet+ajax开发省市区三级联动 技术点:jdbc操作数据库,ajax提交,字符拦截器,三级联动 特点:局部刷新达到省市区三级联动,举一反三可以做商品分类等 宗旨:从实战中学习 博 ...

随机推荐

  1. .NET Core微服务之基于Exceptionless实现分布式日志记录

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.Exceptionless极简介绍 Exceptionless 是一个开源的实时的日志收集框架,它可以应用在基于 ASP.NET,AS ...

  2. python验证码简单识别

    因为需求,所以接触了验证码这一块,原本感觉到会很难,学了之后挺简单的,但后来又发现自己还是too young... PIL(python Image Library) 目前PIL的官方最新版本为1.1 ...

  3. AngularJS 截取字符串

    参考文章:https://blog.csdn.net/u010234516/article/details/54631525 //过滤器 app.filter('textLengthSet', fun ...

  4. LIS3DH三轴加速度计-实现欧拉角(俯仰角,横滚角)

    1. LIS3DH管脚定义 PS:LIS3DH和mpu6050的X和Y方向是相反的, mpu6050如下图所示: 2.LIS3DH加速度计介绍 由于LIS3DH只可以得到XYZ加速度,无法获取角速度, ...

  5. 在docker中初次体验.net core 2.0

    .net core的跨平台有了Linux,不能没有docker……网上的系列文章一大推,特别是docker还有了中文官网:https://www.docker-cn.com/ .上面说的很清楚了,这里 ...

  6. Python数据描述与分析

    在进行数据分析之前,我们需要做的事情是对数据有初步的了解,比如对数据本身的敏感程度,通俗来说就是对数据的分布有大概的理解,此时我们需要工具进行数据的描述,观测数据的形状等:而后才是对数据进行建模分析, ...

  7. 智能ERP收银统计-优惠统计计算规则

    1.报表统计->收银统计->优惠统计规则          第三方平台优惠:(堂食订单:支付宝口碑券优惠)+(外卖订单:商家承担优惠)          自平台优惠:(堂食订单:商家后台优 ...

  8. MongoDB- 简单操作命令

    MongoDB是基于集合操作的数据库 1.进入与退出 mongo / exit 2.库操作 显示所有库: show dbs; 查看当前所在库: db; 切换&使用某个库: use db_nam ...

  9. Linux/Ubuntu 16.04 好用的视频播放器 SMPlayer

    在ubuntu上播放视频是少不了的事情,那么就安装SMPlayer吧, 终端输入 :sudo apt-add-repository ppa:rvm/smplayer                   ...

  10. JavaScript中编码函数escape,encodeURI,encodeURIComponent

    第一:escape():对字符串进行编码,escape()不编码的字符:@*/+ 第二:encodeURI() 函数可把字符串作为 URI 进行编码.不会进行转义的:;/?:@&=+$,# 第 ...