关于ant design 树控件中遇到后台返回数据和jstree要求格式不一致的解决办法

本文介绍了一种将后端返回的复杂层级数据转换为前端所需树形结构的方法,通过递归函数实现了json数据中key、title、children字段的格式转换。

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

按照官网文档上 数据应该是这样子

const 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',
}]

可是在有时候链条的时候 不可能都尽如人意 有时候会需要我们前端自行去处理后台返回来的数据
那么假如后台返回的数据是这样子的

"treeData ": [{
	"id": 82,
	"name": "管理员模块",
	"type": "group",
	"client": [{
		"id": 83,
		"name": "管理员管理",
		"type": "contro",
		"client": [{
			"id": 84,
			"name": "管理员列表",
			"type": "action"
		}, {
			"id": 85,
			"name": "添加页",
			"type": "action"
		}, {
			"id": 86,
			"name": "添加",
			"type": "action"
		}, {
			"id": 87,
			"name": "详情",
			"type": "action"
		}, {
			"id": 88,
			"name": "编辑页",
			"type": "action"
		}, {
			"id": 89,
			"name": "编辑",
			"type": "action"
		}, {
			"id": 90,
			"name": "删除",
			"type": "action"
		}]
	}, {
		"id": 91,
		"name": "节点管理",
		"type": "contro",
		"client": [{
			"id": 92,
			"name": "节点列表",
			"type": "action"
		}, {
			"id": 93,
			"name": "添加页面",
			"type": "action"
		}, {
			"id": 94,
			"name": "添加",
			"type": "action"
		}, {
			"id": 95,
			"name": "详情",
			"type": "action"
		}, {
			"id": 96,
			"name": "编辑页面",
			"type": "action"
		}, {
			"id": 97,
			"name": "编辑",
			"type": "action"
		}, {
			"id": 98,
			"name": "删除",
			"type": "action"
		}]
	}, {
		"id": 99,
		"name": "角色管理",
		"type": "contro",
		"client": [{
			"id": 100,
			"name": "角色列表",
			"type": "action"
		}, {
			"id": 101,
			"name": "添加页面",
			"type": "action"
		}, {
			"id": 102,
			"name": "添加",
			"type": "action"
		}, {
			"id": 103,
			"name": "详情",
			"type": "action"
		}, {
			"id": 104,
			"name": "编辑页面",
			"type": "action"
		}, {
			"id": 105,
			"name": "编辑",
			"type": "action"
		}, {
			"id": 106,
			"name": "删除",
			"type": "action"
		}, {
			"id": 107,
			"name": "权限获取",
			"type": "action"
		}, {
			"id": 108,
			"name": "权限分配",
			"type": "action"
		}]
	}]
}]

那么就需要我们自行把json数据中的key转换为文档上 key title children的格式 我自己采用的办法是这样子的

changeData(data){
      let that = this
      let jsonObj = data
      jsonObj.forEach(function(item){
        item.name && (item.title = item.name)
        item.id && (item.key = item.id)
        delete item.name
        delete item.id
        if(item.client && Array.isArray(item.client)){
          item.children = that.changeData(item.client)
          delete item.client
        }
      })  
      return jsonObj
    },
通过递归调用一步步更新格式 再删除之前的格式 有需要的可以参考下 
### 使用 React Ant Design 实现树形控件 在 React 应用程序中集成并使用 Ant Design 的 `Tree` 组件可以极大地简化开发过程。下面展示了如何安装必要的依赖项以及创建一个简单的树结构。 #### 安装依赖包 为了能够利用 Ant Design 提供的功能,首先需要通过 npm 或 yarn 来安装 antd: ```bash npm install antd --save ``` 或者如果偏好使用 yarn: ```bash yarn add antd ``` #### 导入样式文件 为了让组件正常显示,还需要导入 Ant Design 的 CSS 文件到项目中的入口 JavaScript 文件里(通常是 index.js 或 App.js): ```javascript import 'antd/dist/reset.css'; // 推荐方式,按需加载更少的样式资源 // import 'antd/dist/antd.css'; // 可选,默认全部引入 ``` #### 创建树形控件实例 接下来定义一个新的函数组件来展示一棵基本的树。这里会用到 `useState` 钩子管理状态变化,并且设置了默认展开节点勾选项的行为逻辑。 ```jsx import React, { useState } from 'react'; import { Tree } from 'antd'; const treeData = [ { title: 'Parent Node', key: '0-0', children: [ { title: 'Child Node', key: '0-0-0' }, { title: 'Child Node', key: '0-0-1' } ] } ]; const MyTreeComponent = () => { const [checkedKeys, setCheckedKeys] = useState([]); return ( <Tree checkable onCheck={(checked) => setCheckedKeys(checked)} checkedKeys={checkedKeys} treeData={treeData} /> ); }; ``` 上述代码片段实现了带有复选框功能的基础版本树形菜单[^1]。当用户点击某个节点旁边的复选框时,该操作会被捕获并通过调用 `setCheckedKeys()` 更新当前被选中的键列表。 #### 扩展功能 对于更加复杂的场景,比如支持半选、禁用某些分支等功能,则可以通过配置更多属性来自定义行为。例如设置 `autoExpandParent=false` 让父级会自动打开;或是传递给 `Tree` 更详细的参数对象以控制其外观与交互特性。 此外,在实际应用场景下可能还会涉及到异步加载数据的情况,这时就需要结合 API 请求其他状态管理工具一起工作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值