9

在此处输入图像描述在此处输入图像描述 我是前端开发的新手。在这里,我有下表:

  <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,如果它很大,其他行会受到影响。我怎样才能避免这种情况?

在此处输入图像描述 [![在此处输入图像描述][3]][3]

4

4 回答 4

37

可以使用position: sticky.

检查示例代码

这里将高度设置为主容器。

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

将位置sticky 设置为tr-> th。

thead tr:nth-child(1) th{background: white; position: sticky;top: 0;z-index: 10;}
于 2019-01-21T09:37:11.307 回答
18

尝试使用 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;
}

希望这会有所帮助

于 2019-01-21T09:47:21.767 回答
5

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

tbody{height: 400px; overflow-y: scroll;display:block;}
th { position: sticky; top: 0; }
于 2019-01-21T09:31:15.260 回答
4

使用位置:粘在顶部:0

th {
  background: white;
  position: sticky;
  top: 0; /* Don't forget this, required for the stickiness */
}

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

于 2020-09-18T12:58:11.697 回答