Element UI el-tree 默认选中节点 - 实现菜单权限授权
要实现 el-tree 上默认选中当前数据的 resId 勾选状态,可以通过设置 ':default-checked-keys' 属性来实现。在 'openDialog_dataScope' 方法中,获取到当前数据的 resId,然后将其赋值给 'treeList' 数组,再将 'treeList' 数组设置为 ':default-checked-keys' 的值。
修改代码如下:
function openDialog_dataScope(roleId?: string) { //1016菜单权限2getResList[handleQueryResList] 和 tree
// 查树
handleQueryResTree();
// 查其他
getRoleResList({ roleId })
.then((data: any) => {
Object.assign(formData2, data); ///表单
treeList.splice(0);///清空数组~~~~
treeList.push(...data.data);
// 设置默认选中的节点
treeList.forEach((item: any) => {
treeData.forEach((node: any) => {
if (item.resId === node.resId) {
node.checked = true;
}
});
});
})
.finally(() => {
loading.value = false;
});
dialog2.visible = true;
dialog2.title = '授权菜单';
}
这样,在 el-tree 上就能默认选中当前数据的 resId 勾选状态了。
原文地址: https://www.cveoy.top/t/topic/pkb9 著作权归作者所有。请勿转载和采集!