vue2实现带地区编号和名称的省市县三级联动效果
前端  /  管理员 发布于 6年前   221
我们知道省市区县都有名称和对应的数字唯一编号,使用编号可以更方便查询以及程序处理,我们今天来了解一下使用vue2来实现常见的省市区下拉联动选择效果。
我们的省市区县的数据源来自本站文章《基于Vue2的简易的省市区县三级联动组件》中的districts.js,感谢v-distpicker作者。districts.js中的数据格式大概是这样的:
export default { 100000: { 110000: '北京市', 120000: '天津市', 130000: '河北省', 140000: '山西省', ... }, 130000: { 130100: '石家庄市', 130200: '唐山市', 130300: '秦皇岛市', 130400: '邯郸市', ... }, 130100: { 130102: '长安区', 130104: '桥西区', 130105: '新华区', 130107: '井陉矿区', ... }, ...}
很显然,districts.js导出的是一个key:value形式的json数据串,那么在js中我们就可以很方便的处理json数据串中的关系。
我们使用vue-cli构建项目,需要安装node和vue环境。然后命令行运行: vue init webpack distpicker 构建好项目工程。具体如何操作的请参照vue官网,这些基础的本文不细讲。
现在我们直接编辑App.vue文件。
<template> <div id="app" class="container"> <div class="demo form-inline"> <select name="province" class="form-control" v-model="province.code" @change="getCitys"> <option value="">选择省份</option> <option v-for="(item, index) in provinceList" :value="index" :key="index"> {{ item }} </option> </select> <select name="city" class="form-control" v-show="showcitys" v-model="city.code" @change="getAreas"> <option value="">选择城市</option> <option v-for="(item, index) in cityList" :value="index" :key="index"> {{ item }} </option> </select> <select name="area" class="form-control" v-show="showareas" v-model="area.code" @change="getDistValue"> <option value="">选择区县</option> <option v-for="(item, index) in areaList" :value="index" :key="index"> {{ item }} </option> </select> <button class="btn btn-info" @click="getSelectVal">获取选中值</button> <div style="margin-top:20px;color:red">{{selected}}</div> </div> </div></template>
这是一个简单三个下拉选择器模板,使用v-model可以设置默认值,@change当下拉选择选项后触发的事件。然后每个select下的option是读取districts.js对应的数据。
我们现在来看JS部分,首先使用import导入省市区县数据,注意我们把districts.js文件放在项目的src目录下,然后定义默认编号100000,因为我们第一个(省级)选择框默认要下拉显示所有的省/自治区/直辖市。然后在data()部分设置变量。最后把created()和methods部分的代码加上,完整的代码如下:
import DISTRICTS from './districts';const DEFAULT_CODE = 100000;export default { name: 'App', data() { return { showcitys: false, showareas: false, selected: '', defaultProvince: '湖南省', defaultCity: '长沙市', defaultArea: '岳麓区', province: {}, city: {}, area: {}, provinceList: [], cityList: [], areaList: [] } }, created() { this.provinceList = this.getDistricts(); this.getDefault(); }, methods: { getDefault() { if (this.defaultProvince !== '') { this.showcitys = true; let provinceCode = this.getAreaCode(this.defaultProvince); this.cityList = this.getDistricts(provinceCode); this.province = { code: provinceCode, value: this.defaultProvince } } if (this.defaultCity !== '') { this.showareas = true; let cityCode = this.getAreaCode(this.defaultCity); this.areaList = this.getDistricts(cityCode); this.city = { code: cityCode, value: this.defaultCity } } if (this.defaultArea !== '') { let areaCode = this.getAreaCode(this.defaultArea); this.area = { code: areaCode, value: this.defaultArea } } }, getSelectVal() { this.selected = this.province.value + this.city.value + this.area.value; console.log(this.province.code + '-'+ this.city.code + '-' + this.area.code); }, //名称转代码 nameToCode(name) { for(let x in DISTRICTS) { for(let y in DISTRICTS[x]) { if(name === DISTRICTS[x][y]) { return y } } } }, //获取区域代码 getAreaCode(value) { if(typeof value === 'string') { return this.nameToCode(value); } return value; }, getCodeValue(code, level=1) { if (level == 1) { //省级 return DISTRICTS[DEFAULT_CODE][code]; } else if (level == 2) { let provinceCode = this.province.code; return DISTRICTS[provinceCode][code]; } else { // let cityCode = this.city.code; return DISTRICTS[cityCode][code]; } }, getDistricts(code = DEFAULT_CODE) { return DISTRICTS[code] || [] }, cleanList(name) { this[name] = [] }, getCitys(e) { this.cityList = this.getDistricts(e.target.value); this.cleanList('areas') this.province = this.setData(e.target.value, 1); this.areaList = []; this.showareas = false; this.showcitys = true; }, getAreas (e) { this.areaList = this.getDistricts(e.target.value); this.city = this.setData(e.target.value, 2); this.showareas = true; }, getDistValue (e) { this.area = this.setData(e.target.value, 3); }, setData(code, level = 1) { code = parseInt(code); return { code: code, value: this.getCodeValue(code, level), } }, }}
我们需要实现的效果是:默认显示省级下拉选择框,下拉选项中应该默认载入省级名称,然后当选择省级下拉框中的省份列表(省级)选项时,显示选中省份的城市列表(市级),然后选择市级城市选项,显示选择城市的区县列表(县级)。在选择完每个选项时,我们应该即时记录选项对应的编号和名称。如果在data()部分设置了省市区县的默认值,则三个下拉框都要显示。
运行npm run dev,在浏览器中输入http://localhost:8080查看效果。
来源于helloweba.net122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号