vue3+element Plus 清空el-tree复选框选中项

前提问题: el-tree加了show-checkbox复选框属性后,在选择完复选框后切换,不会自动清空选中内容,要求在切换时清空复选框选中内容,
解决过程: 设置el-tree的ref值,使用setCheckedKeys方法可清空复选框所选中的内容
解决方法:

html:

复制代码
<el-tree
   highlight-current
   :data="props.menuData"
   @node-click="handleClick"
   @node-expand="showPushData"
   ref="treeRef"
   :expand-on-click-node="false"
   node-key="id"
   :default-expanded-keys="props.expandIdList"
   :show-checkbox="state.switchRelate"
   :props="{ disabled: disabledNodeClass, class: customNodeClass }"
   :check-strictly="false"
   @check="getChecked"
>
</el-tree>
<div class="relateBtn">
    <el-button type="primary" @click="switch">切换</el-button>
</div>

js:

复制代码
const switch= () => {
    //清空复选框勾选内容
    treeRef.value.setCheckedKeys([])
}

注意: 1.el-tree的node-key属性一定要有,我第一次写的是node-key="type"时setCheckedKeys未生效,后改为node-key="id"生效。

2.原本写node-key="type"是为了default-expanded-keys属性自动展开时,依据type值展开,但因为这样写setCheckedKeys不生效所以改成了id,default-expanded-keys给的值可以是一个变量,将type为某某值的id全部放到一个数组里传到该属性中也可达到同样的自动展开效果。

相关推荐
程序员大阳18 小时前
副队长Vue3教程(2)--Vite 搭建 Vue 项目,认识目录结构
前端·javascript·vue.js·vite·创建项目·目录结构·项目结构
北城笑笑20 小时前
Vue 110 ,若依SSE流式接口限次空500排查与解决
前端·javascript·vue.js·后端·vue
Patrick_Wilson20 小时前
构建通过了,为什么有些浏览器还会白屏?
前端·javascript·浏览器
AI前端工程狮1 天前
我把 MCP 长任务拆成三个工具,token 账单涨了 7 倍
javascript·人工智能·程序员
mONESY1 天前
DeepAgents 从零上手:别再手搓 Harness,用中间件拼出一个调研团队
javascript
吴长建先生重名了1 天前
一次线上MySQL慢查询Bug的复现与调优全过程
javascript
LVZ1 天前
AI Agent 缺的不是更强的模型,而是一个运行时
前端·javascript·后端
1-1=01 天前
Vue 中 Class 与 Style 绑定
前端·javascript·vue.js
闲来没事抠鼻屎1 天前
Web打印批量出单:vue-print-designer 千页实测
vue.js
用户83134859306981 天前
Vue3的v-if /v-show/v-else /v-else-if:区别、渲染原理、坑点与性能选型
前端·javascript·vue.js