bootstrap 3响应表,固定第一列

CGr*_*oss 19 css html-table responsive-design twitter-bootstrap

我专门针对移动设备,所以我有一个Bootstrap响应表.它只是一个带有引导类"表响应"的div和一个嵌套在表中的表"table table-striped table-bordered table-hover table-condensed".

有没有简单的方法来确保第一列是固定的(不是水平滚动)?在移动设备上,可能每次都会滚动,但第一列包含基本上是表头的内容.

koa*_*dev 41

如果您只定位移动设备,那么这可能对您有用:您可以克隆表格中的第一列并应用,position:absolute以便在滚动表格的其余部分时显示在"前面".

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

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());
    });
});
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是这种方法的工作演示