Bootstrap 4 table with the scrollable body and header fixed

会有一股神秘感。 提交于 2019-12-21 02:46:18

问题


I am new to the front-end-development. Now, Here I have following table ,

  <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>

Now, In this I have used the table-responsive class.

Now, I tried to make this table scrollable by using the

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

But this is not working.

One another thing about the content of the td if it is large the other rows gets affected . So, How can I avoid this scenario ?

Thanks for the help.

[![enter image description here][3]][3]


回答1:


Try using flex, it should be compatible with 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;
}

Hope this will help




回答2:


Can set fixed header by using position: sticky.

Check the sample code.

Here set the height to the main container.

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

Set the postion sticky to the tr-> th.

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



回答3:


add display:block to the table to fix this

tbody{height: 400px; overflow-y: scroll;display:block;}
th { position: sticky; top: 0; }


来源:https://stackoverflow.com/questions/54286705/bootstrap-4-table-with-the-scrollable-body-and-header-fixed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!