bootstrap 3 responsive table with fixed first column

后端 未结 1 1237
无人共我
无人共我 2020-11-27 17:12

I am targeting mobile specifically so I have a Bootstrap responsive table. Its just a div with the bootstrap class \"table-responsive\" and a table nested inside with the cl

相关标签:
1条回答
  • 2020-11-27 17:55

    If you're only targeting mobile devices then this may work for you: you can clone the first column in your table and apply position:absolute so it appears "in front" when you scroll the rest of the table.

    For this you'd need some basic jquery code and a custom CSS class:

    jQuery

    $(function(){
        var $table = $('.table');
        //Make a clone of our table
        var $fixedColumn = $table.clone().insertBefore($table).addClass('fixed-column');
    
        //Remove everything except for first column
        $fixedColumn.find('th:not(:first-child),td:not(:first-child)').remove();
    
        //Match the height of the rows to that of the original table's
        $fixedColumn.find('tr').each(function (i, elem) {
            $(this).height($table.find('tr:eq(' + i + ')').height());
        });
    });
    

    CSS

    .table-responsive>.fixed-column {
        position: absolute;
        display: inline-block;
        width: auto;
        border-right: 1px solid #ffffd;
        background-color: #fff; /* bootstrap v3 fix for fixed column background color*/
    }
    @media(min-width:768px) {
        .table-responsive>.fixed-column {
            display: none;
        }
    }
    

    Here's a working demo for this approach

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