antd YApi 使用Tree组件改造API目录 (二级目录改造成无限级)

import React, { Component } from 'react'
import { Tree,Icon } from 'antd';
import axios from 'axios';
const { TreeNode } = Tree;


export default class Tree3 extends Component {
  state = {
    gData:[],
    expandedKeys: [],
  }
  componentDidMount(){
    axios.get('http://10.26.21.83:3000/mock/91/openapi/doc/queryMenu').then(res=>{
      console.log(res.data)
      this.setState({
        gData:res.data.data
      })
    })
  }

  render() {
    let d = this.state.gData
    const loop = data => data.map((item) => {
      if (item.itemType === 'cat') { //一级目录
        return <TreeNode key={'cat_' + item._id} title={item.name || item.title}>{loop(item.list)}</TreeNode>;
      }else if(item.record_type === 2) { //文件夹类型
        return  (
          <TreeNode
            className={`interface-item-nav2`}
            title={
              <div className="container-title">
                <div className="interface-item" onClick={e => e.stopPropagation()}>
                  <Icon type="folder-open" style={{ marginRight: 5 }} />
                  {item.title || item.name}
                </div>
              </div>
            }
            key={'cat_' + item._id}
          >
            {item.list && loop(item.list)}
          </TreeNode>)
      }else{
        return ( //文档 api类型
          <TreeNode
            className={`interface-item-nav3`}
            title={
              <div className="container-title">{item.title || item.name}</div>
            }
            key={'' + item._id}
          />);
      }
    });
    return (
      <Tree
        className="draggable-tree"
        defaultExpandedKeys={this.state.expandedKeys}
        blockNode
      >
        {loop(d)}
      </Tree>
    );
  }
}

数据结构:

{
  "errcode": 0,
  "errmsg": "成功!",
  "data": [
    {
      "index": 0,
      "_id": 97,
      "name": "公共分类",
      "project_id": 15,
      "desc": "公共分类",
      "uid": 11,
      "add_time": 1557311308,
      "up_time": 1557311308,
      "__v": 0,
      "itemType": "cat",
      "list": [
        {
          "edit_uid": 0,
          "status": "undone",
          "parent_id": null,
          "record_type": 0,
          "interface_type": "dubbo",
          "index": 0,
          "tag": [],
          "_id": 192,
          "catid": 97,
          "title": "getRoleHouseInfos",
          "r_facade": "com.lianjia.mls.datakeeper.housedel.facade.search.customer.Search4CustomerFacade",
          "r_method": "getRoleHouseInfos",
          "method": "GET",
          "project_id": 15,
          "uid": 11,
          "add_time": 1557372229,
          "up_time": 1557372229,
          "list": []
        },
        {
          "edit_uid": 0,
          "status": "undone",
          "parent_id": null,
          "record_type": 0,
          "interface_type": "http",
          "index": 1,
          "tag": [],
          "_id": 211,
          "method": "GET",
          "catid": 97,
          "title": "menu",
          "path": "/api/interface/list_menu",
          "project_id": 15,
          "uid": 11,
          "add_time": 1557390072,
          "up_time": 1557397650,
          "list": []
        },
        {
          "edit_uid": 11,
          "status": "undone",
          "parent_id": null,
          "record_type": 1,
          "interface_type": "http",
          "index": 1,
          "tag": [],
          "_id": 220,
          "title": "wiki目录",
          "catid": 97,
          "project_id": 15,
          "uid": 11,
          "add_time": 1557456457,
          "up_time": 1557457632,
          "list": []
        },
        {
          "edit_uid": 0,
          "status": "undone",
          "parent_id": null,
          "record_type": 2,
          "interface_type": "http",
          "index": 3,
          "tag": [],
          "_id": 271,
          "title": "tree2",
          "catid": 97,
          "project_id": 15,
          "uid": 11,
          "add_time": 1557900913,
          "up_time": 1557900913,
          "list": [
            {
              "edit_uid": 0,
              "status": "undone",
              "parent_id": "271",
              "record_type": 0,
              "interface_type": "http",
              "api_opened": false,
              "index": 0,
              "tag": [],
              "_id": 278,
              "method": "GET",
              "catid": 97,
              "title": "更新或插入接口的富文本2",
              "path": "/openapi/doc/queryAllTabs2",
              "project_id": 15,
              "uid": 11,
              "add_time": 1557900943,
              "up_time": 1557900943
            }
          ]
        },
        {
          "edit_uid": 0,
          "status": "undone",
          "parent_id": null,
          "record_type": 2,
          "interface_type": "http",
          "index": 4,
          "tag": [],
          "_id": 190,
          "title": "tree",
          "catid": 97,
          "project_id": 15,
          "uid": 11,
          "add_time": 1557370827,
          "up_time": 1557370827,
          "list": [
            {
              "edit_uid": 0,
              "status": "undone",
              "parent_id": "190",
              "record_type": 0,
              "interface_type": "http",
              "api_opened": false,
              "index": 0,
              "tag": [],
              "_id": 191,
              "method": "GET",
              "catid": 97,
              "title": "更新或插入接口的富文本",
              "path": "/openapi/doc/insertOrUpdateApiText",
              "project_id": 15,
              "uid": 11,
              "add_time": 1557370838,
              "up_time": 1557370838
            },
            {
              "edit_uid": 0,
              "status": "undone",
              "parent_id": "190",
              "record_type": 1,
              "interface_type": "http",
              "api_opened": false,
              "index": 2,
              "tag": [],
              "_id": 193,
              "title": "tree2",
              "catid": 97,
              "project_id": 15,
              "uid": 11,
              "add_time": 1557373422,
              "up_time": 1557818144
            },
            {
              "edit_uid": 0,
              "status": "undone",
              "parent_id": "190",
              "record_type": 2,
              "interface_type": "http",
              "api_opened": false,
              "index": 3,
              "tag": [],
              "_id": 285,
              "title": "treeChild2",
              "catid": 97,
              "project_id": 15,
              "uid": 11,
              "add_time": 1557900967,
              "up_time": 1557900967,
              "list": [
                {
                  "edit_uid": 0,
                  "status": "undone",
                  "parent_id": "285",
                  "record_type": 1,
                  "interface_type": "http",
                  "api_opened": false,
                  "index": 0,
                  "tag": [],
                  "_id": 292,
                  "title": "h3目录",
                  "catid": 97,
                  "project_id": 15,
                  "uid": 11,
                  "add_time": 1557902644,
                  "up_time": 1557902644
                }
              ]
            }
          ]
        },
        {
          "edit_uid": 0,
          "status": "undone",
          "parent_id": null,
          "record_type": 0,
          "interface_type": "http",
          "index": 5,
          "tag": [],
          "_id": 189,
          "method": "GET",
          "catid": 97,
          "title": "创建目录",
          "path": "/api/interface/add",
          "project_id": 15,
          "uid": 11,
          "add_time": 1557370124,
          "up_time": 1557370124,
          "list": []
        }
      ]
    }
  ]
}

结果:

Ant Design (antd)Tree 组件是一个用于展示层级结构的数据列表,`loadData` 和 `loadedKeys` 是两个关键属性。 1. `loadData`: 这是一个异步加载数据的函数,当你滚动到某个节点并且它还没有被加载过时,会自动触发这个函数。你需要在这个函数里处理数据的获取,并返回一个 Promise 或者直接返回数据数组。例如: ```javascript loadData(node, callback) { // 模拟从服务器获取数据 setTimeout(() => { const { key } = node; const data = ...; // 根据key从服务器获取的数据 callback(data); // 功加载后的回调,传入加载好的数据 }, 500); } ``` 2. `loadedKeys`: 这是一个数组,保存了已经被加载过的节点的 key。当节点的 key 已经在 `loadedKeys` 中,那么就不会再触发 `loadData` 函数。初始可以为空,随着数据加载逐渐填充。 为了启用懒加载,你需要在 Tree 初始化时设置这两个属性: ```jsx import React from 'react'; import { Tree,Spin} from 'antd'; const TreeNode = Tree.TreeNode; function MyTree() { const [data, setData] = React.useState([]); const [loadedKeys, setLoadedKeys] = React.useState([]); const loadChildren = async (node) => { if (!loadedKeys.includes(node.key)) { const newData = await fetchDataFromServer(node.key); // 异步获取节点数据 setData([...data, ...newData]); // 更新数据 setLoadedKeys([...loadedKeys, node.key]); // 添加已加载节点key } }; return ( <Tree loadData={loadChildren} defaultExpandedKeys={['1', '2']} // 可选,默认展开的节点 loading={<Spin size="small" />} onCheck={(checkedNodes) => console.log('check', checkedNodes)} > {data.map(item => ( <TreeNode title={item.name} key={item.key} /> ))} </Tree> ); } export default MyTree; ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值