zoukankan      html  css  js  c++  java
  • 递归tree结构的数据(修改antd tree结构的数据)

    这是要修改的 树结构数据

    const treeData = [
        {
            title: 'parent 1',
            key: '0-0',
            isStop: 0,
            children: [
                {
                    title: 'parent 1-0',
                    key: '0-0-0',
                    isStop: 0,
                    children: [
                        {
                            title: 'leaf-1',
                            key: '0-0-0-0',
                            isStop: 0,
                        },
                        {
                            title: 'leaf-2',
                            key: '0-0-0-1',
                            isStop: 1,
                        },
                    ],
                },
                {
                    title: 'parent 1-1',
                    key: '0-0-1',
                    isStop: 0,
                    children: [
                        {
                            title: 'leaf-3',
                            key: '0-0-1-0',
                            isStop: 1,
                        },
                    ],
                },
            ],
        },
    ];

    目的我们要修改每一层的 title为 ReactNode 的数据给 每一层加一个 Tag 标签;

    例如:key 为  0-0-0-1 的节点

    {
        title: 'leaf-2',
        key: '0-0-0-1',
        isStop: 1,
    }

    改造成

    {
        title: (
            <Space>
                span>{item.title}</span>
                <Tag color='error'>停</Tag>
        </Space>
        ),
        key: '0-0-0-1',
        isStop: 1,
    }                

    递归方法:

    const recursionTreeData = (treeData) => {
        let nodeData = [];
        treeData.forEach(item => {
            if(item.children){
                item.children = recursionTreeData(item.children);
            }
            
            item.title = (
                    <Space>
                        <span>{item.title}</span>
                        <Tag color={item.isStop ? 'error': 'success'}>{item.isStop ? '停': '启'}</Tag>
                    </Space>
            );
            
            nodeData.push(item);
        });
        return nodeData;
    };

    通过 recursionTreeData 处理即可

    效果图:

    完整示例代码(react+antd):

    import React from "react";
    import { Tree, Row, Col, Tag, Space } from 'antd';
    
    const treeData = [
        {
            title: 'parent 1',
            key: '0-0',
            isStop: 0,
            children: [
                {
                    title: 'parent 1-0',
                    key: '0-0-0',
                    isStop: 0,
                    children: [
                        {
                            title: 'leaf-1',
                            key: '0-0-0-0',
                            isStop: 0,
                        },
                        {
                            title: 'leaf-2',
                            key: '0-0-0-1',
                            isStop: 1,
                        },
                    ],
                },
                {
                    title: 'parent 1-1',
                    key: '0-0-1',
                    isStop: 0,
                    children: [
                        {
                            title: 'leaf-3',
                            key: '0-0-1-0',
                            isStop: 1,
                        },
                    ],
                },
            ],
        },
    ];
    
    const recursionTreeData = (treeData) => {
        let nodeData = [];
        treeData.forEach(item => {
            if(item.children){
                item.children = recursionTreeData(item.children);
            }
            
            item.title = (
                    <Space>
                        <span>{item.title}</span>
                        <Tag color={item.isStop ? 'error': 'success'}>{item.isStop ? '停': '启'}</Tag>
                    </Space>
            );
            
            nodeData.push(item);
        });
        return nodeData;
    };
    
    
    console.log('我是递归返回', recursionTreeData(treeData));
    
    
    
    
    
    function MenuPublish(props){
        const onSelect = (selectedKeys, info) => {
            console.log('selected', selectedKeys, info);
        };
        
        const onCheck = (checkedKeys, info) => {
            console.log('onCheck', checkedKeys, info);
        };
        return (
            <div className='MenuPublish'>
                <Row>
                    <Col span={4}>
                        <Tree
                            checkable
                            defaultExpandedKeys={['0-0-0', '0-0-1']}
                            defaultSelectedKeys={['0-0-0', '0-0-1']}
                            defaultCheckedKeys={['0-0-0', '0-0-1']}
                            onSelect={onSelect}
                            onCheck={onCheck}
                            treeData={treeData}
                        />
                    </Col>
                    <Col span={20}>
                        col-20
                    </Col>
                </Row>
            </div>
        );
    }
    
    export default MenuPublish;
    View Code
  • 相关阅读:
    Docker启动ubuntu容器中使用sudo后报错,bash: sudo: command not found
    Redis持久化rdb&aof
    Python3中copy模块常用功能及其他几种copy方式比较
    学习笔记:tkinter模块常用参数(python3)
    Python核心编程第二版(中文).pdf 目录整理
    11、487-3279
    10、Crashing Balloon
    9、Exponentiation
    8、Fire Net
    7、Reverse Root
  • 原文地址:https://www.cnblogs.com/taohuaya/p/13553701.html
Copyright © 2011-2022 走看看