[英]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:问题:
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.