给出以下表示具有行跨距和列跨距的 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);

但不知道如何计算行距.你知道划艇跨度的公式吗?

推荐答案

  • 遍历 node 并计算出一个 node 的深度为level,最大值达maxLevel.

  • 在遍历时,将所有没有子项的项收集到一个items数组中.这些项目可以有rowSpan项,而不是1项.如果您不收集这些项,我们的性能就会受到影响,因为我们需要第二次遍历 node 来计算rowSpan,所以这就像是缓存优化.

  • 循环遍历items,并将rowSpan计算为maxLevel减go node 的level1.

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);
    }
  }
}
<script>
const columns = [{
  title: "Major1",
  children: [{
    title: "Sub1",
    children: [{
      title: "col1"
    }, {
      title: "col2"
    }]
  }, {
    title: "col3"
  }]
}, {
  title: "col4"
}];
</script>

Javascript相关问答推荐

如果使用React Select ,如何将CSS类添加到元素中?

回归函数不会迭代到foreach中的下一个元素

将未等待的未处理的错误promise 转变为警告@ changeTicksAndRejections(由当时的抛出错误创建)

我的YouTube视频没有以html形式显示,以获取免费加密信号

从连接字符串创建客户端时,NodeJS连接到CosmosDB失败

如何获取转换字节的所有8位?

react/redux中的formData在expressjs中返回未定义的req.params.id

Google Apps脚本中的discord邀请API响应的日期解析问题

调用removeEvents不起作用

docx.js:如何在客户端使用文档修补程序

从WooCommerce Checkout Country字段重新排序国家,保持国家同步

v—自动完成不显示 Select 列表中的所有项目

用于编辑CSS样式的Java脚本

如何在coCos2d-x中更正此错误

创建以键值对为有效负载的Redux Reducer时,基于键的类型检测

如何在 Select 文本时停止Click事件?

为什么云存储中的文件不能公开使用?

在Odoo中如何以编程方式在POS中添加产品

JavaScript -复制到剪贴板在Windows计算机上无效

为什么我看到的是回复,而不是我的文档?