当前位置:网站首页>el-cascader 级联选择器动态加载数据及回显数据方法(最全概括)<grootbaby>
el-cascader 级联选择器动态加载数据及回显数据方法(最全概括)<grootbaby>
2022-07-20 00:03:00 【_格鲁特宝宝】
上代码
<el-cascader
v-if="showCascader"
class="form-item-cascader"
ref="myCascader"
v-model="ruleForm.area"
placeholder="请选择地区"
:props="optionProps"
clearable
@change="handleChange"
></el-cascader>
optionProps: {
value: 'code',
label: 'name',
children: 'areaList',
emitPath: true,
lazy: true,
lazyLoad: (node, resolve)=> {
const { level, value } = node; // 获取当前node对象中的level, value属性
this.getArea(level, value, resolve)
}
},
getArea(level, value, resolve) {
let vm = this;
let url = "/area/v2/find-parent-list";
const data = {}
data.parentId = value ? value : 410000000000
vm.common.getdata(vm, url, data, function(res) {
const nodes = res.data
// level == 3 我这里是四级联动,在最后一级不需要下一级,层级从 0 开始
if (level == 3) {
nodes.forEach(item => {
// 当点击最后一级的时候 label 后面不会转圈圈 并把相关值赋值到选择器上
item.leaf = level >= 1
})
}
resolve(nodes)
})
},
handleChange(value) {
if (value && value[0] && value[1] && value[2] && value[3]) {
this.ruleForm.cityCode = value[0]
this.ruleForm.areaCode = value[1]
this.ruleForm.streetCode = value[2]
this.ruleForm.villageCode = value[3]
const Nodes = this.$refs.myCascader.getCheckedNodes()[0]
this.ruleForm.cityName = Nodes.parent.parent.parent.label
this.ruleForm.areaName = Nodes.parent.parent.label
this.ruleForm.streetName = Nodes.parent.label
this.ruleForm.villageName = Nodes.label
}
},
注意点
1、showCascader 是做编辑回显用的。
编辑弹窗展示的时候,有时候并不能正确回显,取消展示重新渲染级联选择器的⽅法最简单。
2、optionProps lazy 及 lazyLoad 是设置懒加载的。
当地区数据比较多时,全部获取数据速度太慢,体验不太好,改为懒加载,一级一级选择地区就能很好避免速度慢的问题。
3、data.parentId = value ? value : 410000000000
首次加载第一级数据,给出所需要的参数,因为首次加载 node.value 没有值,据此判断是不是第一级数据。
4、item.leaf = level >= 1
当点击最后一级的时候 label 后面不会转圈圈,并把相关值赋值到选择器上。
5、@change="handleChange"
change 事件获取选中值,但是只能获取到 code 并不能拿到 name 值
this.$refs.myCascader.getCheckedNodes()[0].label 可以获取到最后一级数据的 name
parent.label 获取倒数第二级的数据的 name
parent.parent.label 获取倒数第三级的数据的 name
依次类推,拿到全部层级的 code 和 name 传给后端接收使用。
边栏推荐
- linux上redis升级
- NFT in the eyes of blackash: the platform is crying for slaughter, and users send money to the door
- TiKV & TiFlash 加速复杂业务查询
- Office共享协作方法——Office共享的正确打开方式、office365白嫖
- [nepctf2022] recurrence
- 3.JS基础
- App application testing methods and testing ideas
- 【Py】对字典进行join连接操作
- 第八章:区间[ %d,%d]内等差素数列,改进等差素数列
- 微信小程序开发学习4(视图与逻辑)
猜你喜欢
使用GRE实现两个内网跨越公网通信
Component architecture project construction - gradle unified, dependent management and configuration
读透业务安全白皮书——未来四大趋势
黑灰产眼中的NFT:平台嗷嗷待宰,用户送钱上门
西门子低代码客户案例|跨越沟通壁垒,解决应用开发效能瓶颈
Why do you say that the maximum single table of MySQL database is 20million? Based on what?
ES6 - promise vs function "callback hell"
cpolar应用实例之助力航运客户远程办公
nextTick那些事儿
YOLOv5训练自己的voc数据集
随机推荐
Bubble sort
3.JS基础
EL&JSTL:EL表达式总结
Circle Game
un7.19:如何在VS-Code中实现多表联查?
Helm — Chart介绍
Redis upgrade on Linux
error: ‘for‘ loop initial declarations are only allowed in C99 mode
matplotlib库的使用
[summary] treasure bag
cpolar应用实例之助力航运客户远程办公
没有了可用Task slot,Flink新增任务会怎样?
【电商运营】试试这5种个性化营销方法,告别无效营销!
Bi analytical thinking of business intelligence: Capital Management in manufacturing industry
Study notes of pytorch deep learning practice: Circular neural network (basic chapter)
Revit(21)Document-常见的API封装
【汇总】百宝囊
Office共享协作方法——Office共享的正确打开方式、office365白嫖
基于短期的风力发电概率预测(Matlab代码实现)
Unable to install cloudera manager agent