项目涉及到了一个地区筛选,同时省市区三级不进行关联,后端要求以对象包数组的格式传参,完整代码

1、先拿到接口返回的地区数据

对数据进行处理,拿到想要的格式

this.provinceOption =res.data.map((item) => {
let name = '';
if (item.name.includes('内蒙古') || item.name.includes('黑龙江')) {
name = item.name.slice(0, 3);
} else {
name = item.name;
}
return {
...item,
name,
};
});

2、vue用element级联选择器插件渲染

<el-cascader v-model="search.area" placeholder="请选择" @change="areaChange" @visible-change="enpVisibleChange"
:options="provinceOption" filterable clearable collapse-tags :props="{
multiple: true,
checkStrictly: true,
label: 'name',
value: 'name',
expandTrigger: 'hover',
children: 'nextLevel',
}" size="midium">
</el-cascader>

 重点来了,将插件返回的数据进行处理

思路:先将每个数组的第一项存起来,将他们转为对象的key,然后再将数组的第二项作为对象中的key,第三项存到对应的数组中

3、将选中的地区返回的数据进行处理,改成接口需要的参数格式

areaChange() {
const countryArray = [];
const countryList = {}
this.search.area.forEach(item => {
if (!countryArray.includes(item[0])) {
countryArray.push(item[0])
}
})
if (countryArray.length > 0) {
countryArray.forEach(item => {
countryList[item] = {}
if (item.includes('市')) {
countryList[item][item] = []
}
this.search.area.forEach(areaItem => {
if (areaItem.length === 2 && areaItem[0].includes('市')) {
if (item === areaItem[0]) {
areaItem.forEach(areaItemChild => {
if (item !== areaItemChild && !countryList[item][item].includes(areaItemChild)) {
countryList[item][item].push(areaItemChild)
}
})
}
} else if (areaItem.length >= 2 && item === areaItem[0]) {
if (!countryList[item][areaItem[1]]) {
countryList[item][areaItem[1]] = []
}
areaItem.forEach(areaItemChild => {
if (item !== areaItemChild && !countryList[item][areaItem[1]].includes(areaItemChild) && areaItem[1] !== areaItemChild) {
countryList[item][areaItem[1]].push(areaItemChild)
}
})
}
})
})
}
this.search.countryList = countryList;
if (!this.enpVisible) {
this.policySearchList();
}
},

以上就是完整的代码和步骤

最新文章

  1. python笔记:windows 下安装 python lxml
  2. cookie实现记住密码
  3. 【转载】[jquery.validate]自定义方法实现&quot;手机号码或者固定电话&quot;的逻辑验证
  4. python项目练习3:万能的XML
  5. Appium原理
  6. npm更换淘宝镜像
  7. [Powershell] 检查IIS设置
  8. STL中的set集合容器进行集合运算:并、交、差实例
  9. 邓_php面试【002】——完整版
  10. zabbix监控-部署(一)
  11. V7000数据恢复(存储文件系统损坏)案例_北亚数据恢复
  12. Docker最全教程——Redis容器化以及排行榜实战(十三)
  13. JAVA集合类兄妹List和Set
  14. Xamarin for Visual Studio下载后的文件路径
  15. C# Json 和对象的相互转换
  16. day13_雷神_前端01
  17. 介绍python中运算符优先级
  18. 加拿大大学排名 by USNews
  19. CMSZU站群管理系统 升级到 v1.8 [源码下载]
  20. Java File.separator

热门文章

  1. Node.js+Koa2+TypeScript技术概览
  2. Node.js学习笔记----day05(MongonDB详情)
  3. Spacemacs换源无效果
  4. Nginx08 通过扩容提升整体吞吐量 nginx平滑升级-添加sticky模块和使用
  5. @mapper注解
  6. CSS特效集锦(9款 , 总有一款是你喜欢的)
  7. vue-cli环境搭建 (超详细保姆级教程)
  8. P27_wxss - 全局样式和局部样式
  9. C语言小程序-天天酷跑类游戏 26号完成
  10. 内网安全之:黄金&amp;白银票据传递域控制器权限维持