增加节点层级,并且下面显示的数据后面有删除按钮
时间: 2024-03-19 13:43:30 浏览: 49
可以在上面的示例代码基础上进行修改,增加节点层级和删除按钮的功能。
以下是示例代码:
```
import React, { useState } from 'react';
import { Tree, Checkbox, List, Button } from 'antd';
const treeData = [
{
title: 'Node 1',
key: '1',
children: [
{
title: 'Node 1-1',
key: '1-1',
children: [
{
title: 'Node 1-1-1',
key: '1-1-1',
},
{
title: 'Node 1-1-2',
key: '1-1-2',
},
],
},
{
title: 'Node 1-2',
key: '1-2',
children: [
{
title: 'Node 1-2-1',
key: '1-2-1',
},
{
title: 'Node 1-2-2',
key: '1-2-2',
},
],
},
],
},
{
title: 'Node 2',
key: '2',
},
];
function TreeDemo() {
const [checkedKeys, setCheckedKeys] = useState([]);
const [treeData, setTreeData] = useState(treeData);
const onCheck = (checkedKeys) => {
setCheckedKeys(checkedKeys);
};
const onAdd = (selectedKey) => {
const newNode = {
title: `New Node ${Math.random().toString(36).substr(2, 5)}`,
key: `${selectedKey}-${Math.random().toString(36).substr(2, 5)}`,
children: [],
};
const traverseTree = (node) => {
if (node.key === selectedKey) {
node.children.push(newNode);
} else if (node.children) {
node.children.forEach((child) => {
traverseTree(child);
});
}
};
treeData.forEach((node) => {
traverseTree(node);
});
setTreeData([...treeData]);
};
const onDelete = (selectedKey) => {
const traverseTree = (node, parentNode) => {
if (node.key === selectedKey) {
if (parentNode) {
parentNode.children = parentNode.children.filter(
(child) => child.key !== selectedKey
);
} else {
setTreeData(treeData.filter((node) => node.key !== selectedKey));
}
} else if (node.children) {
node.children.forEach((child) => {
traverseTree(child, node);
});
}
};
treeData.forEach((node) => {
traverseTree(node, null);
});
setCheckedKeys(checkedKeys.filter((key) => key !== selectedKey));
};
const renderTreeNode = (node) => {
const title = (
<div>
<span>{node.title}</span>
<Button
type="text"
size="small"
danger
onClick={() => onDelete(node.key)}
>
Delete
</Button>
<Button type="text" size="small" onClick={() => onAdd(node.key)}>
Add Child
</Button>
</div>
);
if (node.children) {
return (
<Tree.TreeNode title={title} key={node.key}>
{node.children.map((child) => renderTreeNode(child))}
</Tree.TreeNode>
);
} else {
return <Tree.TreeNode title={title} key={node.key} />;
}
};
const renderCheckedList = () => {
const checkedNodes = [];
const traverseTree = (node) => {
if (checkedKeys.includes(node.key)) {
checkedNodes.push(node.title);
}
if (node.children) {
node.children.forEach((child) => {
traverseTree(child);
});
}
};
treeData.forEach((node) => {
traverseTree(node);
});
return (
<List
header={<div>Checked Nodes</div>}
bordered
dataSource={checkedNodes}
renderItem={(item) => <List.Item>{item}</List.Item>}
/>
);
};
return (
<div>
<Tree checkable checkedKeys={checkedKeys} onCheck={onCheck}>
{treeData.map((node) => renderTreeNode(node))}
</Tree>
{renderCheckedList()}
</div>
);
}
export default TreeDemo;
```
首先在`treeData`中为每个节点添加一个`children`属性,来表示子节点,然后在`renderTreeNode`方法中增加删除按钮和添加子节点按钮,并且分别绑定`onDelete`和`onAdd`方法。
`onDelete`方法用来删除节点,需要遍历整个树形数据,找到要删除的节点,并且将其从父节点的`children`数组中移除。
`onAdd`方法用来添加子节点,需要在当前节点下添加一个新节点,并且更新树形数据。
最后在`Tree`组件中使用`renderTreeNode`方法来渲染树形结构,并且使用`checkedKeys`属性来绑定勾选状态,使用`onCheck`事件来监听勾选状态的变化。
同时,通过`renderCheckedList`方法来渲染勾选的节点列表,遍历整个树形数据,将勾选的节点的标题存储到`checkedNodes`数组中,最后使用`List`组件将列表展示出来。
阅读全文