目录

1、el-tree

2、a-tree


树形组件后端返回数据包含父节点和子节点,但在子节点不全选中时候,不能把父节点勾选上,否则会出现父节点勾选,里边未被选中的子节点也全部被选中 

1、eltree

<el-tree
    :data="menuData"
    :props="treeProp"
    show-checkbox
    :default-expand-all="false"
    node-key="id"
    @check="boxCheck"
    ref="treeRef"
    :default-checked-keys="checkedKeys"
>
</el-tree>

const menuData = ref([])
const treeRef = ref([])
const checkedKeys = ref([])
const treeProp = ref({
    children: 'children',
    label: 'lable'
})

const getRoleMenu = (roleId) => {
    let data = {
        roleId
    }
    http.get('/xxx', data).then(res => {
        if (res.data.code === 200) {
            menuData.value = res.data.data.menus
            // 回显选中菜单
            let menuIds=res.data.data.checkedKeys
            //避免获取不到getNode
            setTimeout(()=>{
                menuIds.forEach((item) => {
                    const node = treeRef.value.getNode(item);
                    if (node.isLeaf) {
                        reeRef.value.setChecked(item, true);
                        chooseKeys.value.push(item)
                       }
                    });
                },100)
            }
        })
    }

const boxCheck = () => {
     let checkedKeys=treeRef.value.getCheckedKeys()
     let halfCheckedKeys=treeRef.value.getHalfCheckedKeys()
     chooseKeys.value=checkedKeys.concat(halfCheckedKeys)
}

//清空选中的项
treeRef.value.setCheckedKeys([], false)

2、a-tree

<a-tree
    v-model:checkedKeys="checkedKeys"
    checkable
    :tree-data="treeData"
    :field-names="fieldNames"
    @check="treeCheck"
>
</a-tree>
const checkedKeys = ref([]);
const newCheckedKeys = ref([])//修改需要的所选keys
const fieldNames = {
    children: 'children',
    title: 'menuName',
    key: 'menuId'
};
const treeData = ref([])
const childNodeId=ref([])//存放所有子节点数组

//获取后端返回菜单数据当前被选中的keys
const getTreeData=()=>{
    httpGet('/xxxx').then(res => {
        if (res.code === 200) {
            treeData.value = res.data.tree
            getDeepNodeId(treeData.value)
            //求交集
            let checkedKeysArr = res.data.checkedKeys
            const result = [...new Set(childNodeId.value)].filter((item) => new Set(eval(checkedKeysArr)).has(item))
            checkedKeys.value = [...result]
        }
    })
}

//循环遍历出最深层子节点(包含所有的子节点及没有子节点的父节点),存放一个数组中
const getDeepNodeId=(data)=>{
    data.map((item) => {
        if (item.children &amp;&amp; item.children.length > 0) {
            getDeepNodeId(item.children);
        } else {
            childNodeId.value.push(item.menuId);
        }
    });
}

const treeCheck = (checkedKeys, info) => {
    //将父节点拼接到子节点,用于修改功能
    //newCheckedKeys.value = checkedKeys.concat(info.halfCheckedKeys);
    newCheckedKeys.value =[...checkedKeys, ...info.halfCheckedKeys]
}

参考1

参考2 

原文地址:https://blog.csdn.net/csdnyp/article/details/127403146

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_26016.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注