当前位置:网站首页>el-tree实现只显示某一级复选框且单选
el-tree实现只显示某一级复选框且单选
2022-04-23 15:24:00 【优雅 的猪】
其实这个东西做出来之后,还是蛮简单的啦。
先说一下实现的思路:
只显示某一级复选框:el-tree中提供了disabled属性,当咱们给某一个节点设置了disabled属性后,那么当前节点就禁用了,所以咱们找到添加disabled的节点复选框的类名,通过display:none隐藏掉就好了,这里我都已经找好了,代码贴在下面。
单选:大家都知道复选框都是多选的,但是需求总是不尽人意,非得单选才行呢,其实也不难。大家在选中之后,拿到当前选中的节点对象,然后重新设置下选中的节点(只设置当前选中的节点,即单选)
关键代码如下:
el-tree: 组件
这里注意最好是都设置node-key 要不然可能会出问题
<el-tree
:data="treeData"
:props="{ children: 'children', label: 'label' }"
show-checkbox
@check-change="checkChange"
ref="Tree"
:check-strictly="true"
node-key="id"
></el-tree>
从后端获取到树形数据之后:
因为我这里需求是只能选择最里层的(也就是没有children的层级)所以采用递归的方式添加disabled属性,没有children的就不添加
// 获取右侧树形数据
getTreeList() {
/**
* ajax 获取到了数据 并 this.treeData = data.data
* 获取到之后
*/
this.setDisabled(this.treeData)
},
// 向有children的节点上增加disabled属性
setDisabled(treeData) {
treeData.forEach(item => {
if(item.children && item.children.length) {
item.disabled = true
this.setDisabled(item.children)
}
})
},
然后css:
这里只要设置这个就可以了,节点上有disabled属性的将不会显示复选框了
/* 隐藏掉el-tree中有disabled属性的框框 */
/deep/.el-checkbox__input.is-disabled{
display: none;
}
然后只能单选:
// 选中复选框触发
checkChange(currObj,isChecked) {
if(isChecked) { // 选中之后我们就重新设置下选中的节点(只设置当前选中的节点,即单选)
this.$refs.Tree.setCheckedNodes([currObj])
}
},
效果:
版权声明
本文为[优雅 的猪]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_51431448/article/details/124349108
边栏推荐
- Kubernetes详解(九)——资源配置清单创建Pod实战
- Reptile exercises (1)
- A series of problems about the best time to buy and sell stocks
- Nacos程序连接MySQL8.0+ NullPointerException
- Example of time complexity calculation
- Three uses of kprobe
- Machine learning - logistic regression
- redis-shake 使用中遇到的错误整理
- JSON date time date format
- setcontext getcontext makecontext swapcontext
猜你喜欢
nuxt项目:全局获取process.env信息
How to design a good API interface?
The wechat applet optimizes the native request through the promise of ES6
Tencent has written a few words, Ali has written them all for a month
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
Kubernetes详解(十一)——标签与标签选择器
Detailed explanation of kubernetes (XI) -- label and label selector
For 22 years, you didn't know the file contained vulnerabilities?
Squid agent
Deep learning - Super parameter setting
随机推荐
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
C language super complete learning route (collection allows you to avoid detours)
like和regexp差别
Mysql连接查询详解
The wechat applet optimizes the native request through the promise of ES6
Example of time complexity calculation
Do keyword search, duplicate keyword search, or do not match
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
Functions (Part I)
如何设计一个良好的API接口?
Introduction to dirty reading, unrepeatable reading and phantom reading
C语言超全学习路线(收藏让你少走弯路)
Openstack command operation
Differential privacy (background)
我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
如何设计一个良好的API接口?
Llvm - generate if else and pH
买卖股票的最佳时机系列问题
Async void caused the program to crash