javascript - 如何使一个元素跨越 React Table 中的整个单元格
问题描述
嗨,我想让我的 td 元素标签占据属于该列的整个单元格。这是我到目前为止得到的输出:
基本上,我希望“独立研究”占据第一栏。到目前为止,这是我的代码:
<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>
我真的很感激一些帮助,因为这个困扰了我这么久。谢谢。
解决方案
修复嵌套的 tr
<Table striped bordered hover>
<thead>
<tr>
<th>{`Courses Taught By ${this.state.professorname}`}</th>
<th>{`Comments on Course`}</th>
</tr>
</thead>
{this.state.courses.map((course, i) => (
<tr>
<td key={course._id}>
<Link to={`/${this.state.id}/${course._id}`}>{course.name}</Link>
</td>
<td>
<table>
<tr>
{this.state.comments[i].map((comment, j) => (
<td key={j}>
<p>{this.state.comments[i][j]}</p>
</td>
))}
</tr>
</table>
</td>
</tr>
))}
</tr>
</Table>
还有其他选项也可以使用 colspan 或者只是将注释添加到 td 作为 p 标签并在 css 中设置样式 这是使用 p 标签的另一个解决方案:
替换这个:
<table>
<tr>
{this.state.comments[i].map((comment, j) => (
<td key={j}>
<p>{this.state.comments[i][j]}</p>
</td>
))}
</tr>
</table>
对此:
{this.state.comments[i].map((comment, j) => (
<p key={j}>{this.state.comments[i][j]}</p>
))}
只需使用 css 对其进行样式设置
推荐阅读
- powershell - VS Code powershell 没有运行任何离子命令
- javascript - 轮询时承诺异步等待
- reactjs - 如何在不中断 onEndReached 的情况下在 ScrollView 中有一个 React FlatList
- android - 检测运行 MDM(移动设备管理)的设备
- c++ - 逐个字符读取文件并使用测试
- java - 终端的 POST 请求中不支持 Spring Boot 内容类型“文本/纯文本”
- python - Python将GTFS时间转换为日期时间
- android - 使用 GestureDetectorCompat 的奇怪行为
- python - 从队列中获取消息时更新标签
- android - Android、Kotlin、ContentResolver:图像查看器无法识别我的应用发送的内容 uri