el-tree限制选中个数的实例
作者:花归去
这篇文章主要介绍了el-tree限制选中个数,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
el-tree限制选中个数
<el-tree style="max-width: 600px" :data="Treedata" :check-strictly="true" show-checkbox node-key="id" :props="defaultProps" :default-expanded-keys="['1', '2']" ref="treeRef" @check="handleCheckChange" />
const handleCheckChange = (data: any, checked: any) => { const selectedNodes = treeRef.value.getCheckedNodes(); if (selectedNodes.length > 3 && checked) { treeRef.value.setChecked(data, false); // 取消选中超出限制的节点 ElMessage.warning(`您最多只能选择 ${3} 个节点!`); } };
const handleCheckChange= (currNode: any, checkedInfo: any) => { let checkedKeys = checkedInfo.checkedKeys || []; let treeNodesMap = treeRef.value.store?.nodesMap || {}; let status = checkedKeys.length >= 3; if (status) { ElMessage.warning(`您最多只能选择 ${3} 个节点!`); } Object.keys(treeNodesMap).forEach((key) => { let item = treeNodesMap[key] || {}; if (!checkedKeys.includes(key)) { let data = item.data || {}; data.disabled = status; treeRef.value.setCurrentNode(data); } }); };
到此这篇关于el-tree限制选中个数的文章就介绍到这了,更多相关el-tree限制选中个数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!