最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Scroll table (but not thead) - Stack Overflow

programmeradmin4浏览0评论

I have a (long) Bootstrap table for which I want to allow for scrolling with overflow: auto;. I only want the tbody to be scrollable, not thead content.

Please see this fiddle for my attempt. I have tried to set a height on tbody unsuccessfully.

I have a (long) Bootstrap table for which I want to allow for scrolling with overflow: auto;. I only want the tbody to be scrollable, not thead content.

Please see this fiddle for my attempt. I have tried to set a height on tbody unsuccessfully.

Share Improve this question edited Feb 4, 2017 at 18:51 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 4, 2013 at 21:58 RandomblueRandomblue 116k150 gold badges362 silver badges557 bronze badges 1
  • That will be a fierce battle to win, bootstrap aside thats actually hard to acplish, specially cross-browser. Anyways it has been discussed lots of times here like in: stackoverflow./questions/1019938/… . A quick search on google will yield several javascript solutions and also many pure CSS ones that will probably work in most browsers ... all of those solutions won't be clean as expected. – mfreitas Commented Feb 4, 2013 at 22:56
Add a ment  | 

2 Answers 2

Reset to default 5

You can't set tbody as scrollable. You need to create two separate tables -- one for the header and one for the body. Wrap the body table in a div and set the overflow-y property to scroll.

Updated jsfiddle: http://jsfiddle/SzGW3/37/

Markup:

<div class="scrollable">
    <table class="table table-hover table-striped">
        <thead>
            <tr>
                <th>User</th>
                <th>Rights</th>
                <th></th>
            </tr>
        </thead>
    </table>
</div>
<div class="bodycontainer scrollable">
    <table class="table table-hover table">
        <tbody id="user-rows">
            <tr class="user-row" style="opacity: 1;">
                <td>Justin</td>
                <td>Administrator</td>
                <td><span class="btn btn-mini btn-primary edit pull-right fade">Edit</span>
                </td>
            </tr>
            <tr class="user-row" style="opacity: 1;">
                <td>Sebastien</td>
                <td>Administrator</td>
                <td><span class="btn btn-mini btn-primary edit pull-right fade">Edit</span>
                </td>
            </tr>
            <tr class="user-row" style="opacity: 1;">
                <td>Steve</td>
                <td>Operator</td>
                <td><span class="btn btn-mini btn-primary edit pull-right fade">Edit</span>
                </td>
            </tr>
            <tr class="user-row" style="opacity: 1;">
                <td>Thomas</td>
                <td>Administrator</td>
                <td><span class="btn btn-mini btn-primary edit pull-right fade">Edit</span>
                </td>
            </tr>
            <tr class="user-row" style="opacity: 1;">
                <td>admin</td>
                <td>Administrator</td>
                <td><span class="btn btn-mini btn-primary edit pull-right fade">Edit</span>
                </td>
            </tr>
        </tbody>
    </table>
</div>

Style:

@import url('http://twitter.github./bootstrap/assets/css/bootstrap.css');

.scrollable {
    overflow-y: scroll;
}

.bodycontainer {
    height: 100px !important;
    width: 100%;
}

The scroll bar on the header is to keep the widths the same. You can fix that with some JavaScript, but there should also be some nice JS / JQuery libraries you can find to handle the entire tbody scrolling issue for you.

If you want to set the same width for header cols (th) and cols (td):

table thead>tr>th{
    width: 20% !important; /* 20% for 5, 25% for 4, etc */

}

table tbody>tr>td{
    width: 20% !important; /* 20% for 5, 25% for 4, etc */
}

This might be helpful in your case:

http://www.novasoftware./download/jquery_fixedtable/jquery_fixedtable.aspx

Also i give a try at fiddle it could give you a hint:

One single row and column and a div inside with overflow:auto and a fixed height and another table inside:

<tbody id="user-rows">
   <tr>
     <td>
        <div style="overflow:auto; height:10em; width:20em;">
          <table >
              <tr>
               .
               .
               . 
              </tr>
          </table>
发布评论

评论列表(0)

  1. 暂无评论