antd react拿到树结构选中的数据,树结构多层穿梭框(上)

本文展示了如何在antd中使用React实现树结构的穿梭框,并获取选中的数据。当前示例仅能将选中项覆盖添加到右侧,后续将探讨如何追加数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果大概如下,可以自己拿代码试试,这个现在是只能拿到选中的添加到右边,并且会覆盖右边的数据,再研究下追加到右边,有需要的可以自己再封装成通用组件

树结构穿梭框下篇,可追加数据
在这里插入图片描述

import React, {
    Component } from 'react';
import {
    Tree, Input, Button } from 'antd';
// import requset from '@/utils/request';
import {
    NuCard } from '@cbay/cbay-ui';
import {
    cloneDeep } from 'lodash';
const {
    Search } = Input;
//扁平后数据
let arr = [];
class TransferTree extends Component {
   
    state = {
   
        treeData: [],
        expandedKeys: [],
        searchValue: '',
        autoExpandParent: true,
        loading: false,
        rightTree: [],
        rightTreeCopy: [],
        checkedKeysLeft: [],
        leftBtnDisabled: true,
        rightBtnDisabled: true
    };

    componentDidMount() {
   
        //请求树结构
        //如果没有给父节点key,那就给每个子节点设置父节点key
        const newDATA = cloneDeep(DATA);
        this.setParent(newDATA);
        // console.log(DATA);
        //扁平数据
        this.setnode(newDATA);
        // console.log(arr);
    }

    //1设置父节点id
    setParent = treeData => {
   
        treeData.map(t => {
   
            if (t.children && t.children.length) {
   
                t.children.map(c => {
   
                    c.parentKey = t.key;
                });
                this.setParent(t.children);
            } else {
   
                return;
            }
        });
    };
    //2扁平数据
    setnode = c => {
   
        c.map(item => {
   
            arr.push(item);
            item.children && this.setnode(item.children);
        });
    };

    onSelect = (selectedKeys, info) => {
   
        console.log(info);
        const {
    type, eventKey } = info.node.props;
        this.setState({
   
            selected: {
   
                type,
                id: eventKey
            }
        });
    };

    checked = (checkedkeys, node) => {
   
        const {
    halfCheckedKeys } = node || {
   };
        //设置左边复选框选中的key
        this.setState({
   
            checkedKeysLeft: {
   
                checked: checkedkeys,
                halfChecked: halfCheckedKeys
            }
        });
        //
        let checkedAllLeftKey = checkedkeys.concat(halfCheckedKeys);

        // const rightTreeCopy = this.ListToTree(selectValue);
        const rightTreeCopy = this.ListToTree(checkedAllLeftKey);
        this.setState({
   
            rightTreeCopy,
            checkedAllLeftKey,
            leftBtnDisabled: !checkedkeys.length
        });
        // console.log(rightTreeCopy);
    };

    checkedRight = checkedkeys => {
   
        const {
    checkedAllLeftKey } = this.state;
        let checkAllCopy =</
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值