需求
最近在做关于地区的的选择,使用的是element-ui
中的el-cascader级联选择器
控件,要按照大区->省->市这种关系进行组合,控件需要的内容是:
[{
value: 'zhinan',
label: '指南',
children: [{
value: 'shejiyuanze',
label: '设计原则',
children: [{
value: 'yizhi',
label: '一致'
}, {
value: 'fankui',
label: '反馈'
}, {
value: 'xiaolv',
label: '效率'
}, {
value: 'kekong',
label: '可控'
}]
}, {
value: 'daohang',
label: '导航',
children: [{
value: 'cexiangdaohang',
label: '侧向导航'
}, {
value: 'dingbudaohang',
label: '顶部导航'
}]
}]
}, {
value: 'zujian',
label: '组件',
children: [{
value: 'basic',
label: 'Basic',
children: [{
value: 'layout',
label: 'Layout 布局'
}, {
value: 'color',
label: 'Color 色彩'
}, {
value: 'typography',
label: 'Typography 字体'
}, {
value: 'icon',
label: 'Icon 图标'
}, {
value: 'button',
label: 'Button 按钮'
}]
}, {
value: 'form',
label: 'Form',
children: [{
value: 'radio',
label: 'Radio 单选框'
}, {
value: 'checkbox',
label: 'Checkbox 多选框'
}, {
value: 'input',
label: 'Input 输入框'
}, {
value: 'input-number',
label: 'InputNumber 计数器'
}, {
value: 'select',
label: 'Select 选择器'
}, {
value: 'cascader',
label: 'Cascader 级联选择器'
}, {
value: 'switch',
label: 'Switch 开关'
}, {
value: 'slider',
label: 'Slider 滑块'
}, {
value: 'time-picker',
label: 'TimePicker 时间选择器'
}, {
value: 'date-picker',
label: 'DatePicker 日期选择器'
}, {
value: 'datetime-picker',
label: 'DateTimePicker 日期时间选择器'
}, {
value: 'upload',
label: 'Upload 上传'
}, {
value: 'rate',
label: 'Rate 评分'
}, {
value: 'form',
label: 'Form 表单'
}]
}]
解决办法
我使用的是Administrative-divisions-of-China这个库的数据,但是没有大区的,所以首先定义大区(自己定义)
region.json
// provinces:省的code码
[{
"code": "10001",
"name": "东北",
"provinces": ["21", "22", "23"]
}, {
"code": "10002",
"name": "华东",
"provinces": ["31", "32", "33", "34", "35", "36", "37"]
}, {
"code": "10003",
"name": "华北",
"provinces": ["11", "12", "13", "14", "15"]
}, {
"code": "10004",
"name": "华中",
"provinces": ["41", "42", "43"]
}, {
"code": "10005",
"name": "华南",
"provinces": ["44", "45", "46"]
}, {
"code": "10006",
"name": "西南",
"provinces": ["50", "51", "52", "53", "54"]
}, {
"code": "10007",
"name": "西北",
"provinces": ["61", "62", "63", "64", "65"]
}]
省和市的json在Administrative-divisions-of-China中可以下载,因为不需要用到直辖市的区,所以我分别下载省级和地级的json
文件,自己拼接了
index.js
代码如下
var fs = require("fs");
var cityFile = "./city.json";
var provincesFile = "./provinces.json";
var regionFile = "./region.json";
//同步加载json文件
var cityRes = JSON.parse(fs.readFileSync(cityFile));
var provincesRes = JSON.parse(fs.readFileSync(provincesFile));
var regionRes = JSON.parse(fs.readFileSync(regionFile));
//组合省和市的数据
var arr = provincesRes.map(provincesVal => {
provincesVal.children = [];
cityRes.map(cityVal => {
if (provincesVal.code === cityVal.provinceCode) {
delete cityVal.provinceCode;
provincesVal.children.push(cityVal);
}
});
return provincesVal;
});
//组合大区、省和市的数据
var address = regionRes.map(val => {
val.children = [];
val.provinces.forEach(element => {
arr.map(val1 => {
if (element === val1.code) {
val.children.push(val1);
}
});
});
delete val.provinces;
return val;
});
//把数据写入文件
fs.writeFile("address.json", JSON.stringify(address), function(err) {
if (err) {
return console.error(err);
}
console.log("数据写入成功!");
fs.readFile("address.json", function(err, data) {
if (err) {
return console.error(err);
}
console.log("异步读取文件数据: " + data.toString());
});
});
使用
执行
node index.js
因为生成json
中的key
跟控件中不一样,这边我直接使用编译器去全局替换了
code --> value
name --> label
最后导入json
文件使用,效果如下