【问题标题】:Bootstrap table with fixed header and no scroll on body具有固定标题且在正文上没有滚动的引导表
【发布时间】:2018-04-23 16:02:37
【问题描述】:

我正在尝试以自定义方式创建表格组件,我将表格 <thead> 固定,<tbody> 可滚动而无需正文滚动。

  1. 问题是我不知道如何为保持表格的div.content-table 设置高度,以便始终保持相同的高度尺寸但不大于屏幕尺寸,所以我无法添加px 或其他类型的固定大小。

  2. 我不知道为什么位置固定对<thead> 不起作用:|

fiddle example:

【问题讨论】:

    标签: javascript twitter-bootstrap css html-table


    【解决方案1】:

    尝试添加

    jquery-1.12.4.js,

    jquery.dataTables.min.js

    &jquery.dataTables.min.css

    更新JSFiddle

    $(document).ready(function() {
    	$('#example').DataTable();
    
    })
    .header {
      align-items: center;
      background: orange none repeat scroll 0 0;
      display: flex;
      flex-flow: column nowrap;
      margin-bottom: 20px;
      padding: 20px;
      position: fixed;
      width: 100%;
    }
    .content{
      padding-top: 140px;
    }
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    <div class="header">
      <h3 class="heading">
    We have a heading here !
    </h3>
      <p class="small">And this is a text</p>
    </div>
    
    <div class="content">
      <div class="table-responsive">
        <table id="example" class="table table-bordered">
          <thead>
            <tr>
              <th>#</th>
              <th>Table heading</th>
              <th>Table heading</th>
              <th>Table heading</th>
              <th>Table heading</th>
              <th>Table heading</th>
              <th>Table heading</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th scope="row">1</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">2</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">3</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">4</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">5</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">6</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">7</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">8</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">9</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">10</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">11</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">12</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">13</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">14</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">15</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">16</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">17</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
            <tr>
              <th scope="row">18</th>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
              <td>Table cell</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    【讨论】:

    • 我正在使用 Angular,我无法添加 jQuery。
    【解决方案2】:

    1。不大于屏幕尺寸

    您可以为此使用vw,vh 单位。

    1vh = 1% * screenHeight
    1vw = 1% * screenWidth
    

    所以你的情况

    max-height = calc(100vh - 200px);
    max-width = 100vw;
    

    2。为什么位置固定不起作用

    如果你使用

    thead{
      position:fixed;
    }
    

    thead 将与tbody 分开工作,我认为 css 在position:fixed 元素上单独计算。

    更多信息 table header fixed example

    【讨论】:

    • 问题是我已经为容器使用了 100vh,但我忘记添加带有 - 标题高度的计算。我认为你的建议会奏效:)
    • 要使您的表头固定,请参阅此链接,它将对您有所帮助。 codepen.io/tjvantoll/pen/JEKIu
    • 这是一个很好的例子,但我发现了一个非常简单的方法,它就像魅力一样工作:table thead tr{ display:block; } 表 th,表 td{ 宽度:100px; } 表 tbody{ 显示:块;高度:200px;溢出:自动; }
    • 很高兴听到这个消息
    猜你喜欢
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多