简体   繁体   English

使用 rowspan 创建动态表

[英]Create dynamic table with rowspan

I am working with React and trying to create a dynamic table.我正在使用 React 并尝试创建一个动态表。 The table looks like this.桌子看起来像这样。

在此处输入图片说明

I am working with this object:我正在处理这个对象:

[
{category: "Type One", subCategory: "One", val: 1, rowSpan: 4},
{category: "Type One", subCategory: "Two", val: 2, rowSpan: 4},
{category: "Type One", subCategory: "three", val: 3, rowSpan: 4},
{category: "Type One", subCategory: "Four", val: 4, rowSpan: 4},
{category: "Type Two", subCategory: "Five", val: 5, rowSpan: 2},
{category: "Type Two", subCategory: "Six", val: 6, rowSpan: 2},
{category: "Type Three", subCategory: "Seven", val: 7, rowSpan: 1},
{category: "Type Four", subCategory: "Eight", val: 8, rowSpan: 1},
]

This object is dynamic in nature and we may get upto 200 rows.这个对象本质上是动态的,我们最多可以得到 200 行。 each row will have similar structure.每行将具有相似的结构。

I have something like this in mind:我有这样的想法:

body = myData.map(function(value) {
    return (
      <TableRow>
        <TableCell> { value.category} </TableCell>
        <TableCell> { value.subCategory} </TableCell>
        <TableCell> { value.val} </TableCell>
      </TableRow>
    )
  })

<Table>
   <TableBody>
       {body}
   </TableBody>
</Table>

Questions:问题:

  1. How can I set the row-span for first column to achieve desired result?如何设置第一列的行跨度以达到所需的结果?
  2. Does it make sense to create a table to get the desired result or I can use DIV with bootstrap ?创建表格以获得所需的结果是否有意义,或者我可以将 DIV 与 bootstrap 一起使用?

Assuming you are rendering a <table> , the <td> element has a rowspan attribute you can use:假设你正在渲染一个<table><td>元素有一个rowspan属性,你可以使用:

<td rowspan={...}>

Of course, only generate that cell for the first row of a "category":当然,只为“类别”的第一行生成该单元格:

body = myData.map((value, i) => {
  const firstOfCategory = (i == 0) || (value.category != myData[i-1].category); 
  return (
    <tr>
      { firstOfCategory && <td rowspan={value.rowSpan}>{value.category}</td> }
      <td>{value.subCategory}</td>
      ...
    </tr>
  );
});

It look like this:它看起来像这样:

 Football Dragon League 4С…4 Dragon League 4С…4 Football FIFA 20. Amateur daily league Football FIFA 21. eSports Battle. International B FIFA 21. eSports Battle. International B Football FIFA. Elite League Football Kazakhstan. Premier League Kazakhstan. Premier League Football KLASK

ie duplicated values are not going to the second columns.即重复的值不会进入第二列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM