给出以下表示具有行跨距和列跨距的 node 的2D矩阵的示例HTML表
th, td { border: 1px solid black }
<table>
<thead>
<tr>
<th colspan="3">Major 1</th>
<th rowspan="3">col4</th>
</tr>
<tr>
<th colspan="2">Sub 1</th>
<th rowspan="2">col3</th>
</tr>
<tr>
<th>col1</th>
<th>col2</th>
</tr>
</thead>
</table>
As you can see in the example there are some filler rowspans, e.g. for "col4". It needs to fill the remaining height个
这个2D矩阵是动态的,我需要基于来自配置的树状 struct 来计算它.上例的初始树状 struct 将是
const columns = [{
title: "Major1",
children: [{
title: "Sub1",
children: [{
title: "col1"
}, {
title: "col2"
}]
}, {
title: "col3"
}]
}, {
title: "col4"
}];
首先,我希望遍历该 struct 并计算每个 node 的列跨距和行跨距.对于Colspan,我认为公式是
Colspan=子Colspan的总和
但我还想不出行跨度的公式.我开始为Colspans实现算法
const columns = [{
title: "Major1",
children: [{
title: "Sub1",
children: [{
title: "col1"
}, {
title: "col2"
}]
}, {
title: "col3"
}]
}, {
title: "col4"
}];
function traverse(currentColumns) {
for (const currentColumn of currentColumns) {
currentColumn.colspan = 1;
currentColumn.rowspan = 1;
if (currentColumn.children) {
traverse(currentColumn.children);
currentColumn.colspan = currentColumn.children.reduce((totalColspan, childColumn) => totalColspan + childColumn.colspan, 0);
}
}
}
traverse(columns)
console.log(columns);
但不知道如何计算行距.你知道划艇跨度的公式吗?