Twitter引导可滚动表

pan*_*ngi 144 twitter-bootstrap

我想在我的网站上有一张桌子.问题是这个表有大约400行.如何限制桌子的高度,并将滚动条应用于它?这是我的代码:

<div class="span3">
  <h2>Achievements left</h2>
<table class="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Something</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Something</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Something</td>
    </tr>
    <tr>
      <td>4</td>
      <td>Something</td>
    </tr>
    <tr>
      <td>5</td>
      <td>Something</td>
    </tr>
    <tr>
      <td>6</td>
      <td>Something</td>
    </tr>
  </tbody>
</table>

  <p><a class="btn" href="#">View details &raquo;</a></p>
</div>
Run Code Online (Sandbox Code Playgroud)

我试图将max-height和固定高度应用于表,但它不起作用.

Jon*_*ood 231

表元素似乎不直接支持这一点.将表放在div中并设置div的高度并设置overflow: auto.

  • 这实际上有用吗?我尝试了这个,它根本没有效果. (50认同)
  • 仅供参考,将溢出设置为"自动",而不是溢出:滚动,不会创建冗余的水平滚动条. (7认同)
  • @JonathanWood:有没有办法只在表的主体上应用`overflow`,但总是显示`<thead>`部分? (7认同)
  • 只是为了让人清楚...... <div style ="overflow:auto;"> <table class ="table"> .... </ table> </ div> (7认同)
  • 这不起作用...溢出不适用于表. (2认同)
  • @NishchitDhanani:您需要将表包装在div中并在div上设置溢出. (2认同)
  • @JonathanWood你仍然会有桌头,只能滚动身体.我建议将一个表放在"父表"的主体内,只让身体内的div滚动. (2认同)
  • 请举一个小例子! (2认同)

Cho*_*rds 52

.span3 {  
    height: 100px !important;
    overflow: scroll;
}?
Run Code Online (Sandbox Code Playgroud)

你需要将它包装在它自己的div中,或者让span3为它自己的id,这样你就不会影响整个布局.

这是一个小提琴:http://jsfiddle.net/zm6rf/

  • 注意,为`.span3`设置这些属性会影响整个Bootstrap驱动站点上的*all*`span3`元素. (8认同)
  • 请注意'!important'并不重要;) (2认同)

Pim*_*Web 35

不需要将它包裹在div中...

CSS:

tr {
width: 100%;
display: inline-table;
table-layout: fixed;
}

table{
 height:300px;              // <-- Select the height of the table
 display: -moz-groupbox;    // Firefox Bad Effect
}
tbody{
  overflow-y: scroll;      
  height: 200px;            //  <-- Select the height of the body
  width: 100%;
  position: absolute;
}
Run Code Online (Sandbox Code Playgroud)

Bootply:http://www.bootply.com/AgI8LpDugl

  • @Frito别担心,快乐;)我只是忘记了表格布局:固定; ...链接已更新 (4认同)

Tod*_*odd 27

我有同样的问题,并使用上述解决方案的组合(并添加了我自己的扭曲).请注意,我必须指定列宽以使它们在标题和正文之间保持一致.

在我的解决方案中,页眉和页脚在身体滚动时保持固定.

<div class="table-responsive">
    <table class="table table-striped table-hover table-condensed">
        <thead>
            <tr>
                <th width="25%">First Name</th>
                <th width="13%">Last Name</th>
                <th width="25%" class="text-center">Address</th>
                <th width="25%" class="text-center">City</th>
                <th width="4%" class="text-center">State</th>
                <th width="8%" class="text-center">Zip</th>
            </tr>
        </thead>
    </table>
    <div class="bodycontainer scrollable">
        <table class="table table-hover table-striped table-condensed table-scrollable">
            <tbody>
                <!-- add rows here, specifying same widths as in header, at least on one row -->
            </tbody>
        </table>
    </div>
    <table class="table table-hover table-striped table-condensed">
        <tfoot>
            <!-- add your footer here... -->
        </tfoot>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

然后只应用以下CSS:

.bodycontainer { max-height: 450px; width: 100%; margin: 0; overflow-y: auto; }
.table-scrollable { margin: 0; padding: 0; }
Run Code Online (Sandbox Code Playgroud)

我希望这有助于其他人.


Jas*_*son 9

我最近有引导和angularjs要做到这一点,我创建了一个angularjs指令,它解决了这个问题,你可以看到这个工作的例子和细节的博客文章.

只需在表标记中添加fixed-header属性即可使用它:

<table class="table table-bordered" fixed-header>
...
</table>
Run Code Online (Sandbox Code Playgroud)

如果你没有使用angularjs,你可以调整指令的javascript并直接使用它.


Ter*_*rry 8

CSS

.achievements-wrapper { height: 300px; overflow: auto; }
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="span3 achievements-wrapper">
    <h2>Achievements left</h2>
    <table class="table table-striped">
    ...
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 通过这种方法,标题也是可滚动的,那么有没有办法修复表头? (4认同)

Rez*_*ati 5

将表放在 div 中,并为该 div 指定 class pre-scrollable