Last active
August 24, 2024 15:24
-
-
Save afc163/7582f35654fd03d5be7009444345ea17 to your computer and use it in GitHub Desktop.
Address options for antd cascader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import provinces from 'china-division/dist/provinces.json'; | |
import cities from 'china-division/dist/cities.json'; | |
import areas from 'china-division/dist/areas.json'; | |
areas.forEach((area) => { | |
const matchCity = cities.filter(city => city.code === area.cityCode)[0]; | |
if (matchCity) { | |
matchCity.children = matchCity.children || []; | |
matchCity.children.push({ | |
label: area.name, | |
value: area.code, | |
}); | |
} | |
}); | |
cities.forEach((city) => { | |
const matchProvince = provinces.filter(province => province.code === city.provinceCode)[0]; | |
if (matchProvince) { | |
matchProvince.children = matchProvince.children || []; | |
matchProvince.children.push({ | |
label: city.name, | |
value: city.code, | |
children: city.children, | |
}); | |
} | |
}); | |
const options = provinces.map(province => ({ | |
label: province.name, | |
value: province.code, | |
children: province.children, | |
})); | |
export default options; |
https://gitee.com/lazytai/codes/sqwk1nbtrp5gae7u463fz41
加了香港台湾的,试试!其实 china-division 这个包已经包含了港澳台行政区了,只不过没有行政区编码,可以在一楼的基础上用下面的代码插入港澳台地区的行政区:
import hkmotw from 'china-division/dist/HK-MO-TW.json' // 合并港澳台行政区 const _hkmotw = Object.entries(hkmotw).map(([provinceName, provinceItem]) => { return { label: provinceName, value: (Math.random() * 1e10).toFixed(), children: Object.entries(provinceItem).map(([cityName, cityItem]) => { return { label: cityName, value: (Math.random() * 1e10).toFixed(), children: cityItem.map(area => { return { label: area, value: (Math.random() * 1e10).toFixed() } }) } }) } }) options = options.concat(_hkmotw)value 为随机生成的数字,避免影响 cascader 组件的选择。
感谢,有帮助~~
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这个过时了,没办法拉下来就使用,我写了个新的