2016-12-26 43 views
0

我的表体是在以下小提琴https://jsfiddle.net/zj36whmo/给滚动条来表

<table class="table table-striped"> 
 
     <thead> 
 
     <tr> 
 
      <th>Make</th> 
 
      <th>Model</th> 
 
      <th>Color</th> 
 
      <th>Year</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr> 
 
      <td class="filterable-cell">Ford</td> 
 
      <td class="filterable-cell">Escort</td> 
 
      <td class="filterable-cell">Blue</td> 
 
      <td class="filterable-cell">2000</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="filterable-cell">Ford</td> 
 
      <td class="filterable-cell">Escort</td> 
 
      <td class="filterable-cell">Blue</td> 
 
      <td class="filterable-cell">2000</td> 
 
     </tr> 
 
       <tr> 
 
      <td class="filterable-cell">Ford</td> 
 
      <td class="filterable-cell">Escort</td> 
 
      <td class="filterable-cell">Blue</td> 
 
      <td class="filterable-cell">2000</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="filterable-cell">Ford</td> 
 
      <td class="filterable-cell">Escort</td> 
 
      <td class="filterable-cell">Blue</td> 
 
      <td class="filterable-cell">2000</td> 
 
     </tr> 
 
     </tbody> 
 
     
 
    </table>

我想单独给TBODY滚动条。我已经看到了很多关于stackoverflow的问题,让tbody单独滚动。但是,他们混淆了宽度。我试着修改选项,但我失去了滚动。请帮助我

+0

检查这一个[固定表头(http://stackoverflow.com/questions/35071546/fixed-table-header) – Abood

+0

看一看到http://计算器。 com/questions/17067294/html-table-with-100-width-with-vertical-scroll-inside-tbody – gaetanoM

回答

0

编辑根据评论寻找对齐,请注意,许多事情发生了变化,jQuery代码已添加适当的大小。这是根据您的情况改编的suggested question in comments

var $table = $('table'), 
 
    $bodyCells = $table.find('tbody tr:first').children(), 
 
    colWidth; 
 

 
// Adjust the width of thead cells when window resizes 
 
$(window).resize(function() { 
 
    // Get the tbody columns width array 
 
    colWidth = $bodyCells.map(function() { 
 
    return $(this).width(); 
 
    }).get(); 
 

 
    // Set the width of thead columns 
 
    $table.find('thead tr').children().each(function(i, v) { 
 
    $(v).width(colWidth[i]); 
 
    }); 
 
}).resize(); // Trigger resize handler
table { 
 
    width: 100%; 
 
    border-spacing: 0; 
 
    border: 1px solid gray; 
 
} 
 
table tbody, 
 
table thead { 
 
    display: block; 
 
} 
 
thead tr th { 
 
    height: 30px; 
 
    line-height: 30px; 
 
    /* text-align: left; */ 
 
} 
 
table tbody { 
 
    height: 100px; 
 
    overflow-y: auto; 
 
    overflow-x: hidden; 
 
} 
 
tbody { 
 
    border-top: 1px solid lightgray; 
 
} 
 
tbody td, 
 
thead th { 
 
    width: 10%; 
 
    border-right: 1px solid lightgray; 
 
} 
 
tbody td:last-child, 
 
thead th:last-child { 
 
    border-right: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table class="table table-striped"> 
 
    <thead> 
 
    <tr> 
 
     <th>Make</th> 
 
     <th>Model</th> 
 
     <th>Color</th> 
 
     <th>Year</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford test</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue more</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    <tr> 
 
     <td class="filterable-cell">Ford</td> 
 
     <td class="filterable-cell">Escort</td> 
 
     <td class="filterable-cell">Blue</td> 
 
     <td class="filterable-cell">2000</td> 
 
    </tr> 
 
    </tbody> 
 

 
</table>

+0

嗨,标题是正确的。但是行比标题更薄 –

+0

问题是我动态生成内容 –

+0

@Echchama编辑为考虑对齐和调整大小的更复杂的情况,请注意,您可能仍需要调整一些值以适应您的实际情况内容。希望有帮助。 – Syden