gpt4 book ai didi

reactjs - react HTML 表格

转载 作者:行者123 更新时间:2023-12-03 23:16:09 26 4
gpt4 key购买 nike

我目前正在尝试在 React 中制作一个 HTML 表格,但有些事情没有按照它应该的方式进行。我确实得到了渲染输出,但错误为: <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>
);
}

数据如下所示:

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}
]
}
];

最佳答案

而不是推送到 array你可以试试返回jsx来自 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>
);
}

关于reactjs - react HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50385680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com