英文:
How to calculate rowspans for a treelike structure representing a HTML table?
问题
I don't see any specific code in the provided text that needs to be translated. It seems you are asking for assistance with a programming problem related to calculating rowspans for an HTML table based on a treelike structure. If you have any questions or need help with the code or algorithm, please feel free to ask in English, and I'll be happy to assist you.
英文:
Given the following example HTML table representing a 2D matrix of nodes with rowspans and colspans
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
th, td { border: 1px solid black }
<!-- language: lang-html -->
<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>
<!-- end snippet -->
As you can see in the example there are some filler rowspans, e.g. for "col4". It needs to fill the remaining height
This 2D matrix is dynamic, I need to calculate it based on a treelike structure coming from a configuration. The initial treelike structure for the example above would be
const columns = [{
title: "Major1",
children: [{
title: "Sub1",
children: [{
title: "col1"
}, {
title: "col2"
}]
}, {
title: "col3"
}]
}, {
title: "col4"
}];
In the first place I want to traverse this structure and calculate the colspans and rowspans for each node. For the colspan I thought the formula is
> colspan = sum of child colspans
but I couldn't figure out a formula for the rowspan yet. I started implementing the algorithm for colspans
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
<!-- end snippet -->
but don't know how to calculate the rowspans. Do you know the formula for rowspans?
答案1
得分: 1
以下是您要翻译的部分:
-
遍历节点并计算节点的深度
level
,以及达到的最大级别maxLevel
。 -
在遍历过程中,将所有没有子节点的项目收集到一个
items
数组中。这些项目的rowSpan
可能不等于1
。如果不收集这些项目,性能会受到影响,因为我们需要第二次遍历节点来计算rowSpan
,所以这就像是一种缓存优化。 -
循环遍历
items
并计算rowSpan
,计算方法为maxLevel
减去节点的level
再加1
。
希望这对您有所帮助。如果您有任何其他翻译需求,请告诉我。
英文:
-
Traverse the nodes and calc a node's deepness
level
, and a maximum level reachedmaxLevel
. -
While traversing collect all items without children into an
items
array. These items can haverowSpan
different from1
. If you don't collect the items we have a performance hit because we need to traverse the nodes the second time to calculaterowSpan
, so it's like a cache optimization. -
Loop through the
items
and calculaterowSpan
asmaxLevel
minus a node'slevel
plus1
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let maxLevel = 0;
const items = [];
traverse(columns);
items.forEach(item => item.rowSpan = 1 + maxLevel - item.level);
console.log(columns);
function traverse(arr, level = 0){
for(const item of arr){
item.colSpan = 1;
item.level = level;
maxLevel < level && (maxLevel = level);
if(item.children){
item.rowSpan = 1;
traverse(item.children, level + 1);
item.colSpan = item.children.reduce((sum, child) => sum + child.colSpan, 0);
}else{
items.push(item);
}
}
}
<!-- language: lang-html -->
<script>
const columns = [{
title: "Major1",
children: [{
title: "Sub1",
children: [{
title: "col1"
}, {
title: "col2"
}]
}, {
title: "col3"
}]
}, {
title: "col4"
}];
</script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论