且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有固定第一列的引导程序 3 响应表

更新时间:2023-09-18 19:56:16

如果您只针对移动设备,那么这可能对您有用:您可以克隆表中的第一列并应用 position:absolute 所以当你滚动表格的其余部分时它会出现在前面".

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.

为此,您需要一些基本的 jquery 代码和自定义 CSS 类:

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 #ddd;
    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