vue省市区三级联动
仿照小米之家做的一个省市区三级联动,先上代码:
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省市区三级联动的更多相关文章
- vue省市区三级联动(高仿京东)
该栗子是我直接从公司的项目单独拉出来的(懒得重新写一次了),所以代码会有些冗余,下面直接看效果: 接着上代码: html: <template> <div> <div c ...
- vue仿京东省市区三级联动选择组件
工作中需要一个盒京东购物车地址选择相似的一个省市区三级联动选择组件,google查了下都是下拉框形式的,于是自己写了一个,希望对使用vue开发项目的朋友有帮助,显示效果如下:使用vue2.0开发 ht ...
- vue 引用省市区三级联动(element-ui Cascader)
npm 下载 npm install element-china-area-data -S main.js import {provinceAndCityData,regionData,provinc ...
- vue 引用省市区三级联动(插件)
vue 用省市区三级联动之傻瓜式教程(复制粘贴即用) npm 下载 npm install v-distpicker --save main.js //引入 省市区三级联动 import Distpi ...
- JS实现年月日三级联动+省市区三级联动+国家省市三级联动
开篇随笔:最近项目需要用到关于年月日三级联动以及省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例或者只有单纯的js还不完整,却很难找到详细的具体数据(baidu搜索都 ...
- jQuery省市区三级联动插件
体验效果:http://hovertree.com/texiao/bootstrap/4/支持PC和手机移动端. 手机扫描二维码体验效果: 代码如下: <!DOCTYPE html> &l ...
- 省市区三级联动 pickerView
效果图 概述 关于 省市区 三级联动的 pickerView,我想大多数的 iOS 开发者应该都遇到过这样的需求.在遇到这样的需求的时候,大多数人都会觉的这个很复杂,一时无从下手.其实真的没那么复杂. ...
- JS省市区三级联动
不需要访问后台服务器端,不使用Ajax,无刷新,纯JS实现的省市区三级联动. 当省市区数据变动是只需调正js即可. 使用方法: <!DOCTYPE html><html>< ...
- ajax省市区三级联动
jdbc+servlet+ajax开发省市区三级联动 技术点:jdbc操作数据库,ajax提交,字符拦截器,三级联动 特点:局部刷新达到省市区三级联动,举一反三可以做商品分类等 宗旨:从实战中学习 博 ...
随机推荐
- 如何面对被抛弃的System.Data.OracleClient
Visual Studio2012连接访问ORACLE数据库 近些年因工作内容的转变,很少去编码了.一些简单的需求使用VS+SQL SERVER这对老搭档便可快捷而舒服的搞定.只是近日需要管理一些OR ...
- 个人完善的springboot拦截器
import lombok.extern.slf4j.Slf4j; import org.manage.management.permission.interceptor.LoginIntercept ...
- SpringBoot实用小技巧之动态设置SpringBoot日志级别
有时线上问题我们用打日志的方式来观察错误或埋点参数,但由于这些日志如果都打出来会占用大量存储空间而且覆盖了一些有效信息,所以线上级别一般设置INFO,调试级别用作特殊情况下.此时如果线上想查看调试级别 ...
- C++11 (多线程)并发编程总结
| 线程 std::thread 创建std::thread,一般会绑定一个底层的线程.若该thread还绑定好函数对象,则即刻将该函数运行于thread的底层线程. 线程相关的很多默认是move语义 ...
- java基础(五):谈谈java中的多线程
1.多线程 1.1.多线程介绍 学习多线程之前,我们先要了解几个关于多线程有关的概念. 进程:正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一 ...
- C#枚举的简单使用
枚举这个名词大家都听过,很多小伙伴也使用过, 那么枚举在开发中能做什么,使用它后能给程序代码带来什么改变,为什么用枚举. 各位看官且坐下,听我一一道来. 为什么使用枚举? 1.枚举能够使代码更加清晰, ...
- 【Oracle学习笔记】序列
Oracle提供了sequence对象,由系统提供自增长的序列号,通常用于生成数据库数据记录的自增长主键或序号的地方,一般结合触发器使用. Sequence是数据库系统的特性,有的数据库有Sequen ...
- Flask实战第6天:视图函数Response返回值
视图函数的返回值会被自动转换为一个响应对象,Flask的转换逻辑如下: 如果返回的是一个合法的响应对象,则直接返回 可以使用make_response函数来创建Response对象,这个方法可以设置额 ...
- 观察者模式与.Net Framework中的委托与事件
本文文字内容均选自<大话设计模式>一书. 解释:观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够 ...
- 【设计模式】单例模式 Singleton Pattern
通常我们在写程序的时候会碰到一个类只允许在整个系统中只存在一个实例(Instance) 的情况, 比如说我们想做一计数器,统计某些接口调用的次数,通常我们的数据库连接也是只期望有一个实例.Windo ...