先来看看我们请求回来的数据是什么样子的:
city: {
11: {
1101: "市辖区"
}
}
county: {
1101: Object
110101: "东城区"
110102: "西城区"
110105: "朝阳区"
110106: "丰台区"
110107: "石景山区"
}
provincetr:
11: "北京市"
12: "天津市"
13: "河北省"
14: "山西省"
15: "内蒙古自治区"
21: "辽宁省"
22: "吉林省"
23: "黑龙江省"
31: "上海市"
32: "江苏省"
33: "浙江省"
34: "安徽省"
35: "福建省"
36: "江西省"
37: "山东省"
41: "河南省"
42: "湖北省"
43: "湖南省"
44: "广东省"
45: "广西壮族自治区"
46: "海南省"
50: "重庆市"
51: "四川省"
52: "贵州省"
53: "云南省"
54: "西藏自治区"
61: "陕西省"
62: "甘肃省"
63: "青海省"
64: "宁夏回族自治区"
65: "新疆维吾尔自治区"
这里我们使用的是 element-ui 的 el-cascader
<el-form-item label="所在省市区" prop="selectedOptions">
<el-cascader
:options="options"
v-model="selectedOptions"
@change="handleChange"
>
</el-cascader>
</el-form-item>
我们处理的数据如下:
// 处理省市级数据
handlePCDData(res) {
this.province = res.provincetr
this.city = res.city
this.county = res.county
this.options = []
// TODO: 暂时这样处理,后面优化代码
Object.keys(this.province).forEach((key) => {
this.options.push({
value: key,
label: this.province[key],
children: []
})
})
Object.keys(this.city).forEach(key => {
this.options.forEach(item => {
if(key == item.value) {
for(let cityKey in this.city[key]) {
item.children.push({
value: cityKey,
label: this.city[key][cityKey],
children: []
})
}
}
})
})
Object.keys(this.county).forEach(key => {
this.options.forEach(item => {
item.children.forEach(child => {
if(child.value == key) {
for(let countyKey in this.county[key]) {
child.children.push({
value: countyKey,
label: this.county[key][countyKey]
})
}
}
})
})
})
},
写的有点垃圾,暂时能用就行,处理完成之后的数据,大致如下所示:
options: [{
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 表单'
}]
}, {
value: 'data',
label: 'Data',
children: [{
value: 'table',
label: 'Table 表格'
}, {
value: 'tag',
label: 'Tag 标签'
}, {
value: 'progress',
label: 'Progress 进度条'
}, {
value: 'tree',
label: 'Tree 树形控件'
}, {
value: 'pagination',
label: 'Pagination 分页'
}, {
value: 'badge',
label: 'Badge 标记'
}]
}, {
value: 'notice',
label: 'Notice',
children: [{
value: 'alert',
label: 'Alert 警告'
}, {
value: 'loading',
label: 'Loading 加载'
}, {
value: 'message',
label: 'Message 消息提示'
}, {
value: 'message-box',
label: 'MessageBox 弹框'
}, {
value: 'notification',
label: 'Notification 通知'
}]
}, {
value: 'navigation',
label: 'Navigation',
children: [{
value: 'menu',
label: 'NavMenu 导航菜单'
}, {
value: 'tabs',
label: 'Tabs 标签页'
}, {
value: 'breadcrumb',
label: 'Breadcrumb 面包屑'
}, {
value: 'dropdown',
label: 'Dropdown 下拉菜单'
}, {
value: 'steps',
label: 'Steps 步骤条'
}]
}, {
value: 'others',
label: 'Others',
children: [{
value: 'dialog',
label: 'Dialog 对话框'
}, {
value: 'tooltip',
label: 'Tooltip 文字提示'
}, {
value: 'popover',
label: 'Popover 弹出框'
}, {
value: 'card',
label: 'Card 卡片'
}, {
value: 'carousel',
label: 'Carousel 走马灯'
}, {
value: 'collapse',
label: 'Collapse 折叠面板'
}]
}]
}, {
value: 'ziyuan',
label: '资源',
children: [{
value: 'axure',
label: 'Axure Components'
}, {
value: 'sketch',
label: 'Sketch Templates'
}, {
value: 'jiaohu',
label: '组件交互文档'
}]
}], // 省市级联动数据
处理完效果如下:
版权声明:本文为weixin_34907135原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。