赞
踩
npm install element-china-area-data -S
import { regionData, CodeToText, TextToCode } from 'element-china-area-data
regionData是省市区三级联动数据(不带“全部”选项)
CodeToText是个大对象,属性是区域码,属性值是汉字 用法例如:CodeToText[‘110000’]输出北京市
extToCode是个大对象,属性是汉字,属性值是区域码用法例如:TextToCode[‘北京市’].code输出110000,TextToCode[‘北京市’][‘市辖区’].code输出110100,TextToCode[‘北京市’][‘市辖区’][‘朝阳区’].code输出110105
3.CodeToText的使用
数据示例:codeStr=‘110000,110100,110101’, codeArray=[110000,110100,110101]
getCodeToText(codeStr, codeArray) { if (null === codeStr && null === codeArray) { return null; } else if (null === codeArray) { codeArray = codeStr.split(","); } let area = ""; switch (codeArray.length) { case 1: area += CodeToText[codeArray[0]]; break; case 2: area += CodeToText[codeArray[0]] + "/" + CodeToText[codeArray[1]]; break; case 3: area += CodeToText[codeArray[0]] + "/" + CodeToText[codeArray[1]] + "/" + CodeToText[codeArray[2]]; break; default: break; } return area; }
TextToCode 的使用
北京市/市辖区/东城区 转换成 110101在获取详情信息的接口中,使用TextToCode 将字符串转换成编码赋给 selectedOptions
this.selectedOptions = TextToCode[this.addForm.area.split(‘/’)[0]][this.addForm.area.split(‘/’)[1]][this.addForm.area.split(‘/’)[2]].code;
5 整体代码:省市三级联动(不带“全部”选项)
<template> <div id="app"> <el-cascader size="large" :options="options" v-model="selectedOptions" @change="handleChange"> </el-cascader> </div> </template> <script> import { regionData, CodeToText, TextToCode } from 'element-china-area-data' export default { data () { return { options: regionData, selectedOptions: [] } }, methods: { // 在获取详情信息接口中使用 TextToCode 将字符串转换成编码赋给 selectedOptions projectInfo () { var that = this; getProjectInfo({ token: getToken(), id: that.id }).then(res => { this.addForm = { id: res.id, // 基础信息 p_name: res.p_name, //项目名 p_message: res.p_message, //项目信息 area: res.area, //地区 remark: res.remark, //备注 } this.selectedOptions = TextToCode[this.addForm.area.split('/')[0]][this.addForm.area.split('/')[1]][this.addForm.area.split('/')[2]].code; }).catch(err => { Message.error(err) }) }, handleChange (value) { console.log(value) this.getCodeToText(null, value) }, getCodeToText (codeStr, codeArray) { if (null === codeStr && null === codeArray) { return null; } else if (null === codeArray) { codeArray = codeStr.split(","); } let area = ""; switch (codeArray.length) { case 1: area += CodeToText[codeArray[0]]; break; case 2: area += CodeToText[codeArray[0]] + "/" + CodeToText[codeArray[1]]; break; case 3: area += CodeToText[codeArray[0]] + "/" + CodeToText[codeArray[1]] + "/" + CodeToText[codeArray[2]]; break; default: break; } console.log(area) this.addForm.area = area return area; } } } </script>
最后效果:
点击编辑后:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。