Bra*_*ink 4 html-table reactjs
我目前正在尝试使 HTML 表在 React 中工作,但有些事情并没有按照应有的方式进行。我确实得到了渲染输出,但出现错误:<tr> cannot appear as a child of <div>。
我试图在网上找到它,但我使用的技术似乎并不经常使用,所以如果我做错了,请赐教。
先感谢您!
getTableContent = (arr) => {
let result = [];
arr.forEach(function (item, i) {
result.push(
<table key={item.productType}>
<thead>{item.productType}</thead>
<tbody>
{item.contents.forEach(function (nextItem, j) {
result.push(
<tr key={nextItem.type}>
<td>{nextItem.type}</td>
<td>{nextItem.count}</td>
</tr>
)
})}
</tbody>
</table>
);
});
return result;
};
render() {
return (
<div>{this.getTableContent(productSpecification)}</div>
);
}
Run Code Online (Sandbox Code Playgroud)
数据如下:
const productSpecification = [
{
productType: "abc", contents: [
{type: "abc", count: 231},
{type: "abc", count: 56},
{type: "abc", count: 54},
{type: "abc", count: 544},
{type: "abc", count: 54},
{type: "abc", count: 564},
{type: "abc", count: 4},
{type: "abc", count: 4564},
{type: "abc", count: 4531},
{type: "abc", count: 234},
{type: "abc", count: 57},
{type: "abc", count: 7}
]
}
];
Run Code Online (Sandbox Code Playgroud)
array您可以尝试jsx从Try this 返回,而不是推送function。
getTableContent = (arr) => {
const iterateItem = (item) => {
return item.map(function (nextItem, j) {
return (
<tr key={nextItem.type}>
<td>{nextItem.type}</td>
<td>{nextItem.count}</td>
</tr>
);
})
}
return arr.map(function (item, i) {
return (
<table key={item.productType}>
<thead>{item.productType}</thead>
<tbody>
{iterateItem(item.contents)}
</tbody>
</table>
);
});
};
render() {
return (
<div>{this.getTableContent(productSpecification)}</div>
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24324 次 |
| 最近记录: |