Table fixed header and scrollable body

前端 未结 29 1297
粉色の甜心
粉色の甜心 2020-11-22 05:33

I am trying to make a table with fixed header and a scrollable content using the bootstrap 3 table. Unfortunately the solutions I have found does not work with bootstrap or

相关标签:
29条回答
  • 2020-11-22 05:42

    You can place two div where 1st div (Header) will have transparent scroll bar and 2nd div will be have data with visible/auto scroll bar. Sample has angular code snippet for looping through the data.

    Below code worked for me -

    <div id="transparentScrollbarDiv" class="container-fluid" style="overflow-y: scroll;">
        <div class="row">
            <div class="col-lg-3 col-xs-3"><strong>{{col1}}</strong></div>
            <div class="col-lg-6 col-xs-6"><strong>{{col2}}</strong></div>
            <div class="col-lg-3 col-xs-3"><strong>{{col3}}</strong></div>
        </div>
    </div>
    <div class="container-fluid" style="height: 150px; overflow-y: auto">
        <div>
            <div class="row" ng-repeat="row in rows">
                <div class="col-lg-3 col-xs-3">{{row.col1}}</div>
                <div class="col-lg-6 col-xs-6">{{row.col2}}</div>
                <div class="col-lg-3 col-xs-3">{{row.col3}}</div>
            </div>
        </div>
    </div>
    

    Additional style to hide header scroll bar -

    <style>
            #transparentScrollbarDiv::-webkit-scrollbar {
                width: inherit;
            }
    
            /* this targets the default scrollbar (compulsory) */
    
            #transparentScrollbarDiv::-webkit-scrollbar-track {
                background-color: transparent;
            }
    
            /* the new scrollbar will have a flat appearance with the set background color */
    
            #transparentScrollbarDiv::-webkit-scrollbar-thumb {
                background-color: transparent;
            }
    
            /* this will style the thumb, ignoring the track */
    
            #transparentScrollbarDiv::-webkit-scrollbar-button {
                background-color: transparent;
            }
    
            /* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */
    
            #transparentScrollbarDiv::-webkit-scrollbar-corner {
                background-color: transparent;
            }
    
            /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
        </style>
    
    0 讨论(0)
  • 2020-11-22 05:43

    Late to the party (Story of my life), but since this is the first result on google, and none of the above got me working, here's my code

    /*Set a min width where your table start to look like crap*/
    table { min-width: 600px; }
    
    /*The next 3 sections make the magic happen*/
    thead, tbody tr {
        display: table;
        width: 100%;
        table-layout: fixed;
    }
    
    tbody {
        display: block;
        max-height: 200px;
        overflow-x: hidden;
        overflow-y: scroll;
    }
    
    td {
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    /*Use the following to make sure cols align correctly*/
    table, tr, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    
    
    /*Set your columns to where you want them to be, skip the one that you can have resize to any width*/
        th:nth-child(1), td:nth-child(1) {
        width: 85px;
    }
    th:nth-child(2), td:nth-child(2) {
        width: 150px;
    }
    th:nth-child(4), td:nth-child(4) {
        width: 125px;
    }
    th:nth-child(5) {
        width: 102px;
    }
    td:nth-child(5) {
        width: 85px;
    }
    
    0 讨论(0)
  • 2020-11-22 05:43

    The latest addition position:'sticky' would be the simplest solution here

    .outer{
        overflow-y: auto;
        height:100px;
        }
    
    .outer table{
        width: 100%;
        table-layout: fixed; 
        border : 1px solid black;
        border-spacing: 1px;
    }
    
    .outer table th {
            text-align: left;
            top:0;
            position: sticky;
            background-color: white;  
    }
     <div class = "outer">
     <table>
                 <tr >
                  <th>col1</th>
                  <th>col2</th>
                  <th>col3</th>
                  <th>col4</th>
                   <th>col5</th>
                 <tr>
                           
                <tr >
                  <td>data</td>
                  <td>data</td>
                   <td>data</td>
                  <td>data</td>
                  <td>data</td>
                <tr>
                 <tr >
                   <td>data</td>
                  <td>data</td>
                   <td>data</td>
                  <td>data</td>
                  <td>data</td>
                <tr>
                 <tr >
                    <td>data</td>
                  <td>data</td>
                   <td>data</td>
                  <td>data</td>
                  <td>data</td>
                <tr>
                 <tr >
                    <td>data</td>
                  <td>data</td>
                   <td>data</td>
                  <td>data</td>
                  <td>data</td>
                <tr>
                 <tr >
                     <td>data</td>
                  <td>data</td>
                   <td>data</td>
                  <td>data</td>
                  <td>data</td>
                <tr>
                 <tr >
                     <td>data</td>
                  <td>data</td>
                   <td>data</td>
                  <td>data</td>
                  <td>data</td>
                <tr>
     </table>
     </div>

    0 讨论(0)
  • 2020-11-22 05:43

    First add some markup for a bootstrap table. Here I created a striped table but also have added a custom table class .table-scroll which adds vertical scroll bar to the table and makes the table header fixed while scrolling down.

    <div class="col-xs-8 col-xs-offset-2 well">
        <table class="table table-scroll table-striped">
            <thead>
                <tr>
                    <th>#</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>County</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>Andrew</td>
                    <td>Jackson</td>
                    <td>Washington</td>
                </tr>
                <tr>
                    <td>2</td>
                    <td>Thomas</td>
                    <td>Marion</td>
                    <td>Jackson</td>
                </tr>
                <tr>
                    <td>3</td>
                    <td>Benjamin</td>
                    <td>Warren</td>
                    <td>Lincoln</td>
                </tr>
                <tr>
                    <td>4</td>
                    <td>Grant</td>
                    <td>Wayne</td>
                    <td>Union</td>
                </tr>
                <tr>
                    <td>5</td>
                    <td>John</td>
                    <td>Adams</td>
                    <td>Marshall</td>
                </tr>
                <tr>
                    <td>6</td>
                    <td>Morgan</td>
                    <td>Lee</td>
                    <td>Lake</td>
                </tr>
                <tr>
                    <td>7</td>
                    <td>John</td>
                    <td>Henry</td>
                    <td>Brown</td>
                </tr>
                <tr>
                    <td>8</td>
                    <td>William</td>
                    <td>Jacob</td>
                    <td>Orange</td>
                </tr>
                <tr>
                    <td>9</td>
                    <td>Kelly</td>
                    <td>Davidson</td>
                    <td>Taylor</td>
                </tr>
                <tr>
                    <td>10</td>
                    <td>Colleen</td>
                    <td>Hurst</td>
                    <td>Randolph</td>
                </tr>
                <tr>
                    <td>11</td>
                    <td>Rhona</td>
                    <td>Herrod</td>
                    <td>Cumberland</td>
                </tr>
                <tr>
                    <td>12</td>
                    <td>Jane</td>
                    <td>Paul</td>
                    <td>Marshall</td>
                </tr>
                <tr>
                    <td>13</td>
                    <td>Ashton</td>
                    <td>Fox</td>
                    <td>Calhoun</td>
                </tr>
                <tr>
                    <td>14</td>
                    <td>Garrett</td>
                    <td>John</td>
                    <td>Madison</td>
                </tr>
                <tr>
                    <td>15</td>
                    <td>Fredie</td>
                    <td>Winters</td>
                    <td>Washington</td>
                </tr>
            </tbody>
        </table>
    </div>
    

    css

    .table-scroll tbody {
        position: absolute;
        overflow-y: scroll;
        height: 250px;
    }
    
    .table-scroll tr {
        width: 100%;
        table-layout: fixed;
        display: inline-table;
    }
    
    .table-scroll thead > tr > th {
        border: none;
    }
    
    0 讨论(0)
  • 2020-11-22 05:43

    I used the floatThead jQuery plugin (https://mkoryak.github.io/floatThead/#intro)

    docs say it works with Bootstrap 3 tables and I can say it also works with Bootstrap 4 tables with or without the table-responsive helper.

    Using the plugin is as simple as this:

    HTML (vanilla bootstrap table markup)

    <div class="table-responsive">
       <table id="myTable" class="table table-striped">
            <thead>...</thead>
            <tbody>...</tbody>
       </table>
    </div>
    

    Plugin Initialization:

    $(document).ready(function() {
        var tbl=$('#myTable');
        tbl.floatThead({
            responsiveContainer: function(tbl) {
                return tbl.closest('.table-responsive');
            }
        });
    });
    

    Full disclaimer: I am not associated with the plugin in any way. I happened to find it after hours of trying lots of other solutions posted here and elsewhere.

    0 讨论(0)
  • 2020-11-22 05:43

    I made some kinda working CSS only solution by using position: sticky. Should work on evergreen browsers. Try to resize browser. Still have some layout issue in FF, will fix it later, but at least table headers handle vertical and horizontal scrolling. Codepen Example

    0 讨论(0)
提交回复
热议问题