向表格主体添加滚动条

问题描述 投票:39回答:4

我希望尽可能轻松地完成任何其他库。

在我很长的桌子中,我想在<tbody>标签上添加一个滚动条,这样头部始终可见,但它不起作用。你能帮忙吗?

小提琴:http://jsfiddle.net/Hpx4L/

<table border="1">
  <thead>
    <tr>
      <th>Month</th>
      <th>Savings</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <td>Sum</td>
      <td>$180</td>
    </tr>
  </tfoot>
  <tbody style="overflow-y:scroll; height:100px;"> <!-- wont work -->
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
          <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
  </tbody>
</table>
html css
4个回答
61
投票

你可以用可滚动的<tbody>包装<div>的内容:

HTML

....
<tbody>
  <tr>
    <td colspan="2">
      <div class="scrollit">
        <table>
          <tr>
            <td>January</td>
            <td>$100</td>
          </tr>
          <tr>
            <td>February</td>
            <td>$80</td>
          </tr>
          <tr>
            <td>January</td>
            <td>$100</td>
          </tr>
          <tr>
            <td>February</td>
            <td>$80</td>
          </tr>
          ...

CSS

.scrollit {
    overflow:scroll;
    height:100px;
}

看看我的jsfiddle,从你的分叉:http://jsfiddle.net/VTNax/2/


14
投票

这些解决方案通常会出现标题列与主体列对齐的问题,并且在调整大小时可能无法正常工作。我知道你不想使用额外的库,但如果你碰巧使用jQuery,那么这个很小。它支持固定页眉,页脚,列跨越(colspan),水平滚动,调整大小以及在滚动开始之前显示的可选行数。

jQuery.scrollTableBody (GitHub)

只要你有一张适当的qazxsw poi,qazxsw poi和(可选)qazxsw poi的桌子,你需要做的就是:

<thead>

7
投票

这是因为您在<tbody>表中添加了<tfoot>标签,如果没有$('table').scrollTableBody(); ,则无法打印任何数据。

因此,你必须做一个<tbody><td><td>

<div>

制作另一个#header,将充当position: fixed;

 header
 {
      position: fixed;
 }

现在您的标题已修复,正文将滚动。标题将保留在那里。


-1
投票

如果你不想在任何div下包装一个表:

<div>
© www.soinside.com 2019 - 2024. All rights reserved.