树状数据渲染 - davy-gan/web GitHub Wiki

import { Tree } from 'antd';
const { TreeNode } = Tree;

const loop = (data, callback) => (data || []).map((item, index) => callback(item, index));
// 解析导航树
function parseTreeMap(menuTreeMap) {
  // const { menuTreeMap } = props;
  console.log(menuTreeMap, 'menuTreeMap');
  const callback = item => {
    if (item.children && item.children.length > 0) {
      return (
        <TreeNode key={item.kpi_code} title={item.kpi_name} dataRef={item}>
          {loop(item.children, callback)}
        </TreeNode>
      );
    }
    return (
      <TreeNode
        key={item.kpi_code}
        title={item.kpi_name}
        dataRef={item}
        isLeaf={item.has_child_kpi != 1}
      >
        <DrSource id={item.kpi_code} item={item} nodeInfo={item}></DrSource>
      </TreeNode>
    );
  };
  return loop(menuTreeMap, callback);
}

// js 树形结构数据 已知某一子节点 一次向上获取所有父节点
export function treeFindPath(tree, func, path = []) {
  if (!tree) return [];
  // eslint-disable-next-line no-restricted-syntax
  for (const data of tree) {
    // 这里按照你的需求来存放最后返回的内容吧
    path.push(data.kpi_code);

    if (func(data)) return path;

    if (data.children) {
      const findChildren = treeFindPath(data.children, func, path);
      if (findChildren.length) return findChildren;
    }
    path.pop();
  }
  return [];
}
⚠️ **GitHub.com Fallback** ⚠️