javascript - Bootstrap 4 表格,可滚动主体和标题固定

标签 javascript html css reactjs bootstrap-4

enter image description here enter image description here 我是前端开发的新手。这里我有下表:

  <div className="table-responsive">
    <table className="table table-hover" id="job-table">
      <thead>
        <tr className="text-center">
          <th scope="col">Sr.No.</th>
          <th scope="col">Company Name</th>
          <th scope="col">Technology</th>
          <th scope="col">Total Resumes</th>
          <th scope="col">Job Title</th>
          <th scope="col">Total Score</th>
          <th scope="col">Average Score</th>
        </tr>
      </thead>
      <tbody className="text-center">
        {this.props.list && this.props.list.content && this.props.list.content.length > 0 && this.props.list.content.map((item, key) => {
          return (
            <tr key={key}>
              <td className="font-weight-bold">1</td>
              <td className="font-weight-bold">ABCDED</td>
              <td>Software Developer</td>
              <td className="font-weight-bold">17</td>
              <td>5 years experience</td>
              <td className="font-weight-bold">30</td>
              <td className="font-weight-bold">30</td>
            </tr>
          )
        })}
      </tbody>
    </table>
  </div>

在此我使用了table-responsive class。我试图通过使用以下方法使该表可滚动:

tbody { height: 400px; overflow-y: scroll }

但这行不通。

关于 td 内容的另一件事,如果它很大,其他行也会受到影响。我怎样才能避免这种情况?

enter image description here [![在此处输入图片描述][3]][3]

最佳答案

可以使用position: sticky 设置固定标题。

检查 sample code .

这里设置主容器的高度。

.table-responsive{height:400px;overflow:scroll;} 

设置sticky到第tr->th.

thead tr:nth-child(1) th{background: white; position: sticky;top: 0;z-index: 10;}

关于javascript - Bootstrap 4 表格,可滚动主体和标题固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286705/

相关文章:

javascript - 这是不引人注目的 javascript 的缺点之一吗?

javascript - Nest 无法解析对导出服务的依赖关系

jquery - 为数组的每个元素创建 <li>

javascript - 如何在 Bootstrap 4 中实现全高列

css - html5 中的静音标题视频

javascript - jquery旋钮显示输入完成

javascript - 自定义表单验证器在 Angular 1.3 中因 $timeout 和 $setValidity 失败

javascript - 我将如何格式化函数粘贴到文本区域的代码?

javascript - 如何获取所选项目的 id、名称或其他 json 数据?

html - 将光标更改为手指指针