gpt4 book ai didi

javascript - React Table 如何让一个元素跨越整个单元格

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:34 24 4
gpt4 key购买 nike

您好,我想让我的 td 元素标签占据属于该列的整个单元格。这是我到目前为止得到的输出:enter image description here

基本上,我希望“独立学习”占据第一栏。到目前为止,这是我的代码:

                   <Table striped bordered hover>
<thead>
<tr>
<th>{`Courses Taught By ${this.state.professorname}`}</th>
<th>{`Comments on Course`}</th>
</tr>
</thead>

<tr>
{this.state.courses.map((course, i) => (
<tr>
<td key={course._id}>
<Link to={`/${this.state.id}/${course._id}`}>{course.name}</Link>
</td>
{this.state.comments[i].map((comment, j) => (
<td key={j}>
<p>{this.state.comments[i][j]}</p>
</td>
))}
</tr>
))}
</tr>
</Table>

我真的很感激能得到一些帮助,因为这个问题困扰了我很长时间。谢谢。

最佳答案

您基本上有一个双列表,但您的逻辑是如果没有评论则不呈现第二列。您需要做的就是将第二个移出 map 。这样,如果表格单元格为空,您就可以保留它。您还嵌套了无效的 tr > tr,我将包装 tr 更改为 tbody。最后,键应该在行的 tr 上,而不是在第一列的 td 上。希望这会有所帮助。

<Table striped bordered hover>
<thead>
<tr>
<th>{`Courses Taught By ${this.state.professorname}`}</th>
<th>{`Comments on Course`}</th>
</tr>
</thead>
<tbody>
{this.state.courses.map((course, i) => (
<tr key={course._id}>
<td>
<Link to={`/${this.state.id}/${course._id}`}>{course.name</Link>
</td>
<td>
{this.state.comments[i].map((comment, j) => (
<p key={j}>{this.state.comments[i][j]}</p>
))}
</td>
</tr>
))}
</tbody>
</Table>

关于javascript - React Table 如何让一个元素跨越整个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59076908/

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