动态渲染antd树形控件,报Uncaught RangeError: Maximum call stack size exceeded解决方法

根据官方api可知,树形控件接口需要满足如下格式
 treeData = [
  {
    title: '0-0',
    key: '0-0',
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        children: [
          { title: '0-0-0-0', key: '0-0-0-0' },
          { title: '0-0-0-1', key: '0-0-0-1' },
          { title: '0-0-0-2', key: '0-0-0-2' },
        ],
      },
      {
        title: '0-0-1',
        key: '0-0-1',
        children: [
          { title: '0-0-1-0', key: '0-0-1-0' },
          { title: '0-0-1-1', key: '0-0-1-1' },
          { title: '0-0-1-2', key: '0-0-1-2' },
        ],
      },
      {
        title: '0-0-2',
        key: '0-0-2',
      },
    ],
  },
  {
    title: '0-1',
    key: '0-1',
    children: [
      { title: '0-1-0-0', key: '0-1-0-0' },
      { title: '0-1-0-1', key: '0-1-0-1' },
      { title: '0-1-0-2', key: '0-1-0-2' },
    ],
  },
  {
    title: '0-2',
    key: '0-2',
  },
]
此时,我们通过接口请求返回回来的数据可进行此数据结构进行组装,比如我接口返回的数据格式是不含有key和title及children的,就需要组装成下列结构

在这里插入图片描述

具体组装函数如下
一、传统for循环方式
for (let f = 0, fl = res.data.length; f < fl; f++) {
				let item = res.data[f]
				if (item.pId === 0) { // first
					item.children = []
					item.title = item.name
					item.key = item.id
					first.push(item)
					continue // !!!!注意
				}
				for (let s = 0, sl = first.length; s < sl; s++) {
					let sItem = first[s]
					if (item.pId === sItem.id) { // second
						item.children = []
						item.title = item.name
						item.key = item.id
						second.push(item)
						sItem.children.push(item)
					}
				}
				for (let t = 0, tl = second.length; t < tl; t++) {
					let tItem = second[t]
					if (tItem.id == item.pId) { // third
						item.title = item.name
						item.key = item.id
						tItem.children.push(item)
					}
				}
			}
二、使用forEach方式遍历
			res.data.forEach(item => {
				if (item.pId === 0) { // first
					item.children = []
					item.title = item.name
					item.key = item.id
					treeData.push(item)
					return // 改为return
				}
				treeData.forEach(f => {
					if (item.pId === f.id) { // second
						item.children = []
						item.title = item.name
						item.key = item.id
						second.push(item)
						f.children.push(item)
					}
				})
				second.forEach(s => {
					if (s.id == item.pId) { // third
						item.title = item.name
						item.key = item.id
						s.children.push(item)
					}
				})
			})

注意 多层循环时,一定要有跳出循环的条件,否则会报栈溢出!!!,因此在第一层满足条件时加continue或return

数据组装完后,根据官网的树形控件,即可渲染成功

在这里插入图片描述

以下是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> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值