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);
}
});
};