发布时间:2024-02-14 08:30
本文参考:Element UI 中国省市区级联数据
本文参考:根据此文做的整理
npm install element-china-area-data -S
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
数据示例: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;
}
北京市/市辖区/东城区 转换成 110101
在获取详情信息的接口中,使用TextToCode 将字符串转换成编码赋给 selectedOptions
this.selectedOptions = TextToCode[this.addForm.area.split('/')[0]][this.addForm.area.split('/')[1]][this.addForm.area.split('/')[2]].code;
如果想选择任意一级,比如只选省市不选取的话,可以增加条件判断
if (this.addForm.area.split('/')[2]) {
this.selectedOptions = TextToCode[this.addForm.area.split('/')[0]][this.addForm.area.split('/')[1]][this.addForm.area.split('/')[2]].code;
} else {
this.selectedOptions = TextToCode[this.addForm.area.split('/')[0]][this.addForm.area.split('/')[1]].code;
}
如果想选择任意一级,可以使用element-ui的级联选择器中的选择任意一级选项的选择器,给el-cascader添加:props="{ checkStrictly: true }"
属性
<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>
最后效果:
点击编辑后: