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

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. 记一次工作失误,openresty报502错误

    调试落地项目,代理跳转接口报502错误. 一开始认为阿里云tomcat有误,后面发现别的地址代理跳转有效. 开始配置跳转地址,一直折腾半天不好使.后面才知道,应用服务器和数据库服务器是分开部署的.一直 ...

  2. mac 下常用命令备忘录

    1.查看端口号 lsof -i: 2.杀死进程 kill 41321 3.查看文件夹文件 ls ls -l //看到文件及文件夹更多的内容 ls -a //隐藏的文件 ls -la //上面的组合 4 ...

  3. 《前端之路》之二:数据类型转换 && 隐式转换 || 显式转换

    目录 02:数据类型转换 && 隐式转换 || 显式转换 02:数据类型转换 && 隐式转换 || 显式转换 在上一个章节中,我们介绍了 JavaScript 的基本的 ...

  4. 使用strace命令跟踪系统调用

    一.是什么strace? strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由 ...

  5. C#面试考点集锦

    C#面试考点集锦 ©智客坊      岁末年初往往是程序猿准备跳槽的高峰,当然互联网行业跳槽几乎是每个月都在发生,没有太过明显的淡季~那么,如何提高面试的通过率,最终顺利的拿到自己心仪的offer呢? ...

  6. 怎么从Linux服务器上下载超过4G的文件?

    使用sz命令下载文件时,超过4G下载不了,如何下载呢? 本文介绍的方法是先对该文件进行拆分,拆分成多个小于4G的文件,然后分别下载,下载到本地后再进行合并或直接解压,具体操作如下: 1.分拆为多个文件 ...

  7. Django学习之十一:真正理解Django的路由分发和反解url原理

    目录 URL Dispatcher 简介 模式概念 对比URLPattern 与 URLResolver (多态的体现) 构建子路由几种方式 反解url算法逻辑 URL Dispatcher 简介 d ...

  8. 001. Java内存中的字符编码

    Java内存中的字符编码 Unicode字符集及utf-8 .utf-16.utf-32 等字符编码方式 字符集:字符表示的数字集合,元素称为码点或码位: 字符编码:字符实际的储存表示: 码点:一个码 ...

  9. axios 封装

    来自:https://www.jianshu.com/p/68d81da4e1ad 侵删 import axios from 'axios' import qs from 'qs' let baseu ...

  10. 《JavaScript高级程序设计》笔记:DOM(十)

    Node类型 nodeType以下是一些重要的nodeType的取值:1: 元素element2: 属性attr3: 文本text8: 注释comments9: 文档document nodeName ...