具有固定可滚动主体和标题的Bootstrap 4表

gan*_*esh 3 javascript html5 reactjs bootstrap-4

在此处输入图片说明在此处输入图片说明我是新来的front-end-development。现在,这里有下表,

  <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>
Run Code Online (Sandbox Code Playgroud)

现在,在此我使用了table-responsive class

现在,我尝试通过使用

tbody { height: 400px; overflow-y: scroll }
Run Code Online (Sandbox Code Playgroud)

但这是行不通的。

td如果内容很大,另一行会受到影响。因此,如何避免这种情况?

谢谢您的帮助。

在此处输入图片说明 [![在此处输入图片描述] [3]] [3]

Bal*_*731 7

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

检查示例代码

在此设置主容器的高度。

.table-responsive{height:400px;overflow:scroll;} 
Run Code Online (Sandbox Code Playgroud)

将位置设置为tr-> th。

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

  • @Drew我不明白为什么它应该在没有以文本为中心的类的情况下中断。我刚刚测试过,它工作得很好@Balaji731 这个解决方案可能是这里最好的解决方案,因为它不会改变列的宽度。唯一的缺点是`th`的边框没有被保留,并且随着body向上滚动(我们可以在示例代码链接中看到) (2认同)

Hai*_*ham 6

尝试使用flex,它应该兼容table-responsive

table {
    display: flex;
    flex-flow: column;
    width: 100%;
}

thead {
    flex: 0 0 auto;
}

tbody {
    flex: 1 1 auto;
    display: block;
    overflow-y: auto;
    overflow-x: hidden;
}

tr {
    width: 100%;
    display: table;
    table-layout: fixed;
}
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助


Zna*_*war 5

添加display:block到表中以解决此问题

tbody{height: 400px; overflow-y: scroll;display:block;}
th { position: sticky; top: 0; }
Run Code Online (Sandbox Code Playgroud)


Moo*_*Moo 5

使用位置:粘在顶部:0

th {
  background: white;
  position: sticky;
  top: 0; /* Don't forget this, required for the stickiness */
}
Run Code Online (Sandbox Code Playgroud)

完整的示例可以在这里找到: https ://css-tricks.com/position-sticky-and-table-headers/