react 树形表格合并,基于hooks
import React from ‘react’;
import { Checkbox, Row, Col } from ‘antd’;
import PropTypes from ‘prop-types’;
import Table from ‘components/table-antd’;
import ‘./homeStyle.less’;
// 编码Data, 转成表格格式
const encodeData = (data, i = 0, addData = {}) => {
const ret = []
data.forEach(item => {
const next = { [i]: item.name, …addData }
if (item.children) {
ret.push(…encodeData(item.children, i + 1, next))
} else {
ret.push(next)
}
})
return ret
}
// 获取最深的深度以确定列数
const getMaxDepth = data => {
let max = 1
data.forEach(item => {
if (item.children) {
const childDepth = getMaxDepth(item.children)
if (max < 1 + childDepth)
max = 1 + childDepth
}
})
return max
}
// 生成一个子节点map, 用于判断所有
const getChildrenMap = data => {
let ret = {}
data.forEach(item => {
if(item.children){
ret[item.name] = []
const childrenMap = getChildrenMap(item.children)
item.children.forEach(subItem => {
if(childrenMap[subItem.name]){
ret[item.name].push(…childrenMap[subItem.n