vue 城市選擇器的使用 element-china-area-data
阿新 • • 發佈:2020-12-29
技術標籤:Vueelement uivueelementui
一、Element UI 中國省市區級聯資料
本文參考:https://www.npmjs.com/package/element-china-area-data
1. 安裝
npm install element-china-area-data -S
2. 使用
import { provinceAndCityData, regionData, provinceAndCityDataPlus, regionDataPlus, CodeToText, TextToCode } from 'element-china-area-data'
- provinceAndCityData是省市二級聯動資料(不帶“全部”選項)
- regionData是省市區三級聯動資料(不帶“全部”選項)
- provinceAndCityDataPlus是省市區三級聯動資料(帶“全部”選項)
- regionDataPlus是省市區三級聯動資料(帶“全部”選項)
- “全部"選項繫結的value是空字串”"
- CodeToText是個大物件,屬性是區域碼,屬性值是漢字 用法例如:CodeToText[‘110000’]輸出北京市
- extToCode是個大物件,屬性是漢字,屬性值是區域碼
用法例如:TextToCode[‘北京市’].code輸出110000,TextToCode[‘北京市’][‘市轄區’].code輸出110100,TextToCode[‘北京市’][‘市轄區’][‘朝陽區’].code輸出110105
3. 案例
3.1 省市二級聯動(不帶“全部”選項):
<template>
<div id="app">
<el-cascader
size="large"
:options="options"
v-model="selectedOptions"
@change="handleChange">
</el-cascader>
</div>
</template>
<script>
import { provinceAndCityData } from 'element-china-area-data'
export default {
data () {
return {
options: provinceAndCityData,
selectedOptions: []
}
},
methods: {
handleChange (value) {
console.log(value)
}
}
}
</script>
3.2 省市二級聯動(帶“全部”選項):
<template>
<div id="app">
<el-cascader
size="large"
:options="options"
v-model="selectedOptions"
@change="handleChange">
</el-cascader>
</div>
</template>
<script>
import { provinceAndCityDataPlus } from 'element-china-area-data'
export default {
data () {
return {
options: provinceAndCityDataPlus,
selectedOptions: []
}
},
methods: {
handleChange (value) {
console.log(value)
}
}
}
</script>
3.3 省市三級聯動(不帶“全部”選項):
<template>
<div id="app">
<el-cascader
size="large"
:options="options"
v-model="selectedOptions"
@change="handleChange">
</el-cascader>
</div>
</template>
<script>
import { regionData } from 'element-china-area-data'
export default {
data () {
return {
options: regionData,
selectedOptions: []
}
},
methods: {
handleChange (value) {
console.log(value)
}
}
}
</script>
3.4 省市三級聯動(帶“全部”選項):
<template>
<div id="app">
<el-cascader
size="large"
:options="options"
v-model="selectedOptions"
@change="handleChange">
</el-cascader>
</div>
</template>
<script>
import { regionDataPlus } from 'element-china-area-data'
export default {
data () {
return {
options: regionDataPlus,
selectedOptions: []
}
},
methods: {
handleChange (value) {
console.log(value)
}
}
}
</script>
4.繫結value格式
value是區域碼"110000"
二、CodeToText的使用
參考:https://blog.csdn.net/s1441101265/article/details/104297300
資料示例: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;
}
效果: