css - reactjs 创建表格的一部分可滚动

标签 css reactjs html-table

为了使表格的一部分可滚动,我在表格内部使用了一个 div,但收到警告:

Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody>

我理解警告,但我希望表格的一部分(大部分)可以滚动,从而限制表格部分的最大高度:

<div className="main-table">
               <table className="main-edit-table" align="right">
                     <tbody>
                            <tr>
                                 <th>haveri</th>
                             </tr>
                              <div className="inst-container">
                              {this.state.list && this.state.list.map((collection, key) => (
                                    <tr key={key}>
                                        <td>{key+1}</td>
                                        <td>
                                            <div className="main-item-container">
                                                <span>{collection}</span>
                                            </div>
                                        </td>
                                        <td>{collection.subjects[0] && collection.subjects[0].name}</td>
                                    </tr>      
                                    ))}
                                    </div>
                            </tbody>
                        </table>

                    </div>

CSS:

.inst-container {
    border: 1px solid #eeccee;
    max-height: 300px;
    overflow-y: scroll;
    width: 100%;
}

我所做的只是在表格中的标题后插入一个 div,以使表格本身可滚动。

两个问题:

  1. 警告“那么糟糕”吗?我的意思是,我收到了警告,但它工作正常
  2. 您将如何创建标题(可以包含几列)和下方的可滚动表格?使用网格系统是唯一的选择吗?

最佳答案

我宁愿将整个表格包裹在一个带有溢出的 div 容器中,并将列标题设置为粘性位置。

见下文:

<div className="container">
    <table className="main-edit-table" align="right">
        <thead>
            <tr>
                <th className="sticky-column">haveri</th>
            </tr>
        </thead>
        <tbody>
                {this.state.list && this.state.list.map((collection, key) => (
                    <tr key={key}>
                        <td>{key+1}</td>
                         <td>
                             <div className="main-item-container">
                                 <span>{collection}</span>
                             </div>
                             </td>
                             <td>{collection.subjects[0] && 
                             collection.subjects[0].name}</td>
                    </tr>      
                                ))}
        </tbody>
    </table>
</div>

CSS:

.sticky-column {
    position: -webkit-sticky; /* Safari */
    position: sticky;
    top: 0
}
                
.container {
    border: 1px solid #eeccee;
    max-height: 300px;
    overflow-y: scroll;
    width: 100%;
}

查看 CodeSandBox 示例:https://xyv5nl.csb.app/

关于css - reactjs 创建表格的一部分可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54510114/

相关文章:

javascript - 显示条件表行 Angular JS

html - 无法使我的 <table> 属性的 CSS 正常工作

javascript - 两个 <nav> 元素 - 桌面与移动

javascript - 重新启动组件上的 Audio() 流已安装

javascript - Magento 网站 - 按选项排序 : upper and lower arrow is bind with sorting data, 只需要删除上下箭头

javascript - React 不更新 DOM (Laravel)

node.js - 使用 Create React App Boilerplate 进行 AVA 测试运行程序设置

php - 如何从MySQL向表中输入数据?

html - 在 CSS3 中使用 nth-child() 选择 double

css - 如何在 Webpack 中的 HTML 文件中要求 .css、.js 文件