antd树形控件的增删改查
时间: 2023-11-21 12:55:35 浏览: 65
以下是Antd树形控件的增删改查的示例代码:
1. 渲染树形控件
```jsx
import React, { useState } from 'react';
import { Tree, Button, Modal, Form, Input } from 'antd';
const { TreeNode } = Tree;
const Demo = () => {
const [form] = Form.useForm();
const [treeData, setTreeData] = useState([
{
title: 'Node1',
key: '0-0',
children: [
{
title: 'Child Node1',
key: '0-0-0',
},
{
title: 'Child Node2',
key: '0-0-1',
},
],
},
{
title: 'Node2',
key: '0-1',
},
]);
const [visible, setVisible] = useState(false);
const [selectedNode, setSelectedNode] = useState(null);
const handleAdd = () => {
setVisible(true);
};
const handleOk = () => {
form.validateFields().then((values) => {
const newNode = {
title: values.nodeName,
key: `${selectedNode.key}-${selectedNode.children.length}`,
};
setSelectedNode((prev) => {
prev.children.push(newNode);
return { ...prev };
});
setVisible(false);
form.resetFields();
});
};
const handleCancel = () => {
setVisible(false);
form.resetFields();
};
const handleSelect = (selectedKeys, info) => {
setSelectedNode(info.node);
};
const handleDelete = () => {
setSelectedNode((prev) => {
const parentKey = prev.key.split('-').slice(0, -1).join('-'); const newChildren = treeData.find((node) => node.key === parentKey).children.filter((node) => node.key !== prev.key);
const newTreeData = treeData.map((node) => {
if (node.key === parentKey) {
return { ...node, children: newChildren };
}
return node;
});
setTreeData(newTreeData);
return null;
});
};
const handleEdit = () => {
setVisible(true);
form.setFieldsValue({ nodeName: selectedNode.title });
};
return (
<>
<Button onClick={handleAdd}>Add Node</Button>
<Button onClick={handleDelete} disabled={!selectedNode}>
Delete Node
</Button>
<Button onClick={handleEdit} disabled={!selectedNode}>
Edit Node
</Button>
<Tree onSelect={handleSelect} treeData={treeData}>
{renderTreeNodes(treeData)}
</Tree>
<Modal visible={visible} onOk={handleOk} onCancel={handleCancel}>
<Form form={form}>
<Form.Item name="nodeName" label="Node Name" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Form>
</Modal>
</>
);
};
const renderTreeNodes = (data) =>
data.map((item) => {
if (item.children) {
return (
<TreeNode title={item.title} key={item.key} dataRef={item}>
{renderTreeNodes(item.children)}
</TreeNode>
);
}
return <TreeNode {...item} />;
});
export default Demo;
```
2. 添加节点
```jsx
const handleAdd = () => {
setVisible(true);
};
const handleOk = () => {
form.validateFields().then((values) => {
const newNode = {
title: values.nodeName,
key: `${selectedNode.key}-${selectedNode.children.length}`,
};
setSelectedNode((prev) => {
prev.children.push(newNode);
return { ...prev };
});
setVisible(false);
form.resetFields();
});
};
const handleCancel = () => {
setVisible(false);
form.resetFields();
};
```
3. 删除节点
```jsx
const handleDelete = () => {
setSelectedNode((prev) => {
const parentKey = prev.key.split('-').slice(0, -1).join('-');
const newChildren = treeData.find((node) => node.key === parentKey).children.filter((node) => node.key !== prev.key);
const newTreeData = treeData.map((node) => {
if (node.key === parentKey) {
return { ...node, children: newChildren };
}
return node;
});
setTreeData(newTreeData);
return null;
});
};
```
4. 编辑节点
```jsx
const handleEdit = () => {
setVisible(true);
form.setFieldsValue({ nodeName: selectedNode.title });
};
<Modal visible={visible} onOk={handleOk} onCancel={handleCancel}>
<Form form={form}>
<Form.Item name="nodeName" label="Node Name" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Form>
</Modal>
```
阅读全文