element ui tree 树形控件 数据重新赋值 默认选中

element ui tree 树形控件 数据重新赋值 默认选中

需求:当对树形控件的数据进行重新赋值时,当前勾选及展开的状态需要保留。

<template>
    <div>
        <el-tree
            ref="tree"
            :data="data"
            show-checkbox
            node-key="id"
            :default-expanded-keys="expandedKeys"
            :default-checked-keys="checkedKeys"
            :props="defaultProps"
            @node-expand="expand"
            @node-collapse="collapse"
            @check-change="change"
        >
            <span class="custom-tree-node" slot-scope="{ node, data }">
                <span :class="'c' + data.color"
                    >{{ node.label }}----{{ data.color }}</span
                >
            </span>
        </el-tree>
        <el-button @click="submit">submit</el-button>
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        return {
            // 默认展开节点
            expandedKeys: [],
            // 默认选中节点
            checkedKeys: [],
            data: [
                {
                    id: 1,
                    label: "一级 1",
                    color: "1",
                    children: [
                        {
                            id: 4,
                            label: "二级 1-1",
                            color: "2",
                            children: [
                                {
                                    id: 9,
                                    label: "三级 1-1-1",
                                    color: "1"
                                },
                                {
                                    id: 10,
                                    label: "三级 1-1-2",
                                    color: "1"
                                }
                            ]
                        }
                    ]
                },
                {
                    id: 2,
                    label: "一级 2",
                    color: "1",
                    children: [
                        {
                            id: 5,
                            label: "二级 2-1",
                            color: "1"
                        },
                        {
                            id: 6,
                            label: "二级 2-2",
                            color: "1"
                        }
                    ]
                },
                {
                    id: 3,
                    label: "一级 3",
                    color: "1",
                    children: [
                        {
                            id: 7,
                            label: "二级 3-1",
                            color: "1"
                        },
                        {
                            id: 8,
                            label: "二级 3-2",
                            color: "1"
                        }
                    ]
                }
            ],
            defaultProps: {
                children: "children",
                label: "label"
            }
        };
    },
    methods: {
        // 节点选中状态发生变化时的回调
        change(data) {
            let nodes = this.$refs.tree.getCheckedNodes()
            this.checkedKeys = nodes.map(item => {
                return item.id
            })
            console.log(this.checkedKeys);
        },
        //节点被展开时触发的事件
        expand(data) {
            if (!this.expandedKeys.includes(data.id)) {
                this.expandedKeys.push(data.id);
            }
            // console.log(this.expandedKeys);
        },
        // 节点被关闭时触发的事件
        collapse(data) {
            let index = this.expandedKeys.indexOf(data.id)
            if(index >= 0) {
                this.expandedKeys.splice(index, 1)
            }
            // console.log(this.expandedKeys);
        },
        submit() {

            setTimeout(() => {
                console.log(111);
                console.log(this.checkedKeys)
                this.data = [
                {
                    id: 1,
                    label: "一级 1",
                    color: "3",
                    children: [
                        {
                            id: 4,
                            label: "二级 1-1",
                            color: "3",
                            children: [
                                {
                                    id: 9,
                                    label: "三级 1-1-1",
                                    color: "1"
                                },
                                {
                                    id: 10,
                                    label: "三级 1-1-2",
                                    color: "1"
                                }
                            ]
                        }
                    ]
                },
                {
                    id: 2,
                    label: "一级 2",
                    color: "1",
                    children: [
                        {
                            id: 5,
                            label: "二级 2-1",
                            color: "1"
                        },
                        {
                            id: 6,
                            label: "二级 2-2",
                            color: "1"
                        }
                    ]
                },
                {
                    id: 3,
                    label: "一级 3",
                    color: "1",
                    children: [
                        {
                            id: 7,
                            label: "二级 3-1",
                            color: "1"
                        },
                        {
                            id: 8,
                            label: "二级 3-2",
                            color: "1"
                        }
                    ]
                }
            ]
            }, 1000);
        }
    },
    created() {}
};
</script>

<style scoped>
.c1 {
    color: #333;
}
.c2 {
    color: red;
}
.c3 {
    color: rebeccapurple;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yww_yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值