html - 如何固定表头的位置并仅滚动表体

标签 html css angularjs twitter-bootstrap-3

我创建了一个表,其中有 tbody 和 2 tr。其中 1 tr 用于隐藏/显示数据。

我想固定表头的位置并只滚动表体。 因为我在 tbody 上使用 angular js ng-repeat,所以我无法滚动表体

我想要类似这样的输出。 Sample output with scrollable table body

但是我的表结构和这个差不多。下面是代码

<thead>
</thead>
<tbody ng-repeat="a in arr">
 <tr>
 </tr>

 <tr>
 </tr>
<tbody>

HTML 文件

            <thead>
                <tr>
                    <th style="text-align: center;">Edit</th>
                    <th style="text-align: center;">Case ID</th>
                    <th style="text-align: center;">Company Name</th>

                </tr>
                <tr>
                    <th colspan="8"><input  class="form-control"
                        placeholder="Search ..." type="text" id="search" ng- 
                  model="search.compyNmeEnteredByBDO"/></th>
                </tr>
            </thead>
          <tbody ng-repeat="newOpportunity in opportunitycopy >

                <tr>
                  <td>
                <label> <input type="checkbox" value="" ng- 
                          model="newOpportunity.isSelected"> <span
                                class="cr"><i class="cr-icon glyphicon 
                         glyphicon-ok"></i></span>

                            </label>
                  </td>
                    <td>
                        &nbsp;
                        {{newOpportunity.id}}   
                    </td>
                    <td>
                        {{newOpportunity.compyNmeEnteredByBDO}} 
                    </td>




                </tr>

                 <tr ng-show="newOpportunity.hidestatus">

                    <td colspan="8">
                            <div class="row">
                        <div class="col-lg-12 col-md-12">
                            <div class="ibox" id="mailbox-container">
                                <div class="mailbox-header d-flex justify-content-between"
                                    style="border-bottom: 1px solid #e8e8e8;">
                                    <div>
                                        <h5 class="inbox-title">Details</h5>
                                    </div>
                                </div>
                                <div class="mailbox-body">
                                    <form>
                                        <div class="row">
                                        <div class="col-sm-2 form-group">
                                                <label>Quantum</label> <input type="text" readonly
                                                    class="form-control" value="{{newOpportunity.quntm}}">
                                            </div>
                                        </div>
                                        <div class="row">

                                        </div>
                                        <div class="row">
                                        <div class="col-sm-2 form-group">
                                                <label>Remarks</label> <input type="text" readonly
                                                    class="form-control" value="{{newOpportunity.remarks}}">
                                            </div>
                            </div>

                                </div>
                            </div>
                        </div>
                    </div>


                </tr>
            </tbody>

最佳答案

您可以通过设置 position: sticky;

来做到这一点

table thead tr th {
  background-color: orange;
  position: sticky;
  top: -1px;
}
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<table class="table">
  <thead>
    <tr>
      <th>column1</th>
      <th>column2</th>
      <th>column3</th>
      <th>column4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
    <tr>
      <td>td</td>
      <td>td</td>
      <td>td</td>
      <td>td</td>
    </tr>
  </tbody>

</table>

关于html - 如何固定表头的位置并仅滚动表体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57141834/

相关文章:

javascript - 在 React.js 中更改组件的父级

javascript - 如何在 Angularjs 中同步两个同时 $interval promise

angularjs - 对所有 CRUD 操作使用相同的 Controller (类似 Rails)

html - 页脚上可见的 Bootstrap 旋转木马按钮

javascript - 使用 javascript DOM 生成 Html

php - 更改 HTML <area> 元素的背景颜色

HTML 下拉菜单在 OSX 上显示不同

javascript - 背景图片在 iPad Safari 中不可见

javascript - cordova/phonegap 应用程序 - 从服务器加载 js css 和 html

angularjs - 如何在 Restangular 和 Django Rest Framework 中添加分页?