Fix columns in horizontal scrolling

前端 未结 3 1886
情歌与酒
情歌与酒 2020-12-01 03:58

I am currently trying to fix my first column in a table when the user scrolls in the X-axis. I am using this structure:

相关标签:
3条回答
  • 2020-12-01 04:14

    Demo: http://www.jqueryscript.net/demo/jQuery-Plugin-For-Fixed-Table-Header-Footer-Columns-TableHeadFixer/

    HTML

    <h2>TableHeadFixer Fix Left Column</h2>
    
    <div id="parent">
        <table id="fixTable" class="table">
            <thead>
                <tr>
                    <th>Ano</th>
                    <th>Jan</th>
                    <th>Fev</th>
                    <th>Mar</th>
                    <th>Abr</th>
                    <th>Maio</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>2012</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>550.00</td>
                </tr>
                <tr>
                    <td>2012</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>550.00</td>
                </tr>
                <tr>
                    <td>2012</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>550.00</td>
                </tr>
                <tr>
                    <td>2012</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>550.00</td>
                </tr>
                <tr>
                    <td>2012</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>110.00</td>
                    <td>550.00</td>
                </tr>
            </tbody>
        </table>
    </div>
    

    JS

        $(document).ready(function() {
            $("#fixTable").tableHeadFixer({"head" : false, "right" : 1}); 
        });
    

    CSS

        #parent {
            height: 300px;
        }
    
        #fixTable {
            width: 1800px !important;
        }
    

    https://jsfiddle.net/5gfuqqc4/

    0 讨论(0)
  • 2020-12-01 04:22

    SOLVED

    http://jsfiddle.net/DJqPf/7/

    .table-wrapper { 
        overflow-x:scroll;
        overflow-y:visible;
        width:250px;
        margin-left: 120px;
    }
    td, th {
        padding: 5px 20px;
        width: 100px;
    }
    th:first-child {
        position: fixed;
        left: 5px
    }
    

    UPDATE

    $(function () {  
      $('.table-wrapper tr').each(function () {
        var tr = $(this),
            h = 0;
        tr.children().each(function () {
          var td = $(this),
              tdh = td.height();
          if (tdh > h) h = tdh;
        });
        tr.css({height: h + 'px'});
      });
    });
    body {
        position: relative;
    }
    .table-wrapper { 
        overflow-x:scroll;
        overflow-y:visible;
        width:200px;
        margin-left: 120px;
    }
    
    
    td, th {
        padding: 5px 20px;
        width: 100px;
    }
    tbody tr {
      
    }
    th:first-child {
        position: absolute;
        left: 5px
    }
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
      <meta charset="utf-8">
      <title>JS Bin</title>
    </head>
    <body>
    <div>
        <h1>SOME RANDOM TEXT</h1>
    </div>
    <div class="table-wrapper">
        <table id="consumption-data" class="data">
            <thead class="header">
                <tr>
                    <th>Month</th>
                    <th>Item 1</th>
                    <th>Item 2</th>
                    <th>Item 3</th>
                    <th>Item 4</th>
                </tr>
            </thead>
            <tbody class="results">
                <tr>
                    <th>Jan is an awesome month</th>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                </tr>
                <tr>
                    <th>Feb</th>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                </tr>
                <tr>
                    <th>Mar</th>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                </tr>
                <tr>
                    <th>Apr</th>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>  
                </tr>
                <tr>    
                    <th>May</th>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                </tr>
                <tr>
                    <th>Jun</th>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                    <td>3163</td>
                </tr>
    
                <tr>
                    <th>...</th>
                    <td>...</td>
                    <td>...</td>
                    <td>...</td>
                    <td>...</td>
                </tr>
            </tbody>
        </table>
    </div>
    
    <div>
    </div>
    </body>
    </html>

    0 讨论(0)
  • 2020-12-01 04:25

    Solved using JavaScript + jQuery! I just need similar solution to my project but current solution with HTML and CSS is not ok for me because there is issue with column height + I need more then one column to be fixed. So I create simple javascript solution using jQuery

    You can try it here https://jsfiddle.net/kindrosker/ffwqvntj/

    All you need is setup home many columsn will be fixed in data-count-fixed-columns parameter

    <table class="table" data-count-fixed-columns="2" cellpadding="0" cellspacing="0">
    

    and run js function

    app_handle_listing_horisontal_scroll($('#table-listing'))
    
    0 讨论(0)
提交回复
热议问题