【问题标题】:bootstrap 3 responsive table with fixed first column具有固定第一列的引导 3 响应表
【发布时间】:2013-11-13 06:37:48
【问题描述】:

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

是否有任何简单的方法可以确保第一列是固定的(不是水平滚动)?在移动设备上,可能每次都会滚动,但第一列包含本质上是表格标题的内容。

【问题讨论】:

标签: css twitter-bootstrap html-table responsive-design


【解决方案1】:

如果您只定位移动设备,那么这可能对您有用:您可以克隆表格中的第一列并应用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());
    });
});

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;
    }
}

这里有一个 working demo 用于这种方法

【讨论】:

  • 谢谢,这真的很接近(考虑到我有限的 jquery 知识,我不会想到这一点)但是在有很多行的情况下(因此垂直滚动),它看起来像这样: i.imgur.com/j725O8r.png
  • @CGross 尝试使用position:absolute; 而不是固定
  • 这似乎不适用于 bootstrap 3.2。这是一个修改过的小提琴,其中固定列与原始表重叠:jsfiddle.net/gmsa/nt7fd965/1
  • @Gus 似乎早期 v3 为响应式表格指定了白色背景颜色,但现在已将其删除,您只需将其添加到 CSS 中,它应该看起来不错:jsfiddle.net/koala_dev/nt7fd965/2
  • @koala_dev 你的工作演示(jsfiddle.net/4XG7T/3)不能在移动设备上工作!?
猜你喜欢
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 2014-12-21
  • 1970-01-01
相关资源
最近更新 更多