【问题标题】:responsive table with fixed column具有固定列的响应表
【发布时间】:2014-06-11 12:58:47
【问题描述】:

我在 7 个月前曾发布过关于此问题的帖子,有人很友好地为我指出了正确的解决方案: bootstrap 3 responsive table with fixed first column

但是,这突然停止工作,固定列不再固定。 要查看它的实际效果,请访问:http://nasgame.apphb.com (示例数据:搜索 Matt Vincent 并选择 Pro)

第一列现在随着其余内容滚动。

这是操作代码:

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;
}
@media(min-width:768px) {
.table-responsive>.fixed-column {
    display: none;
}
}

【问题讨论】:

    标签: jquery css twitter-bootstrap twitter-bootstrap-3


    【解决方案1】:

    在我看来它可以正常工作,但是由于某些克隆的表格单元格具有透明背景,您可以看到它们后面的表格:

    在我的屏幕截图中,您可以看到奇数行看起来不错,因为单元格的背景是灰色的。

    您也可以通过给偶数单元格添加背景来解决此问题。

    我认为这可能会做到:

    .table-striped > tbody > tr:nth-child(even) > td {
        background-color: #fff;
    }
    

    或者,正如 3rror404 在评论中建议的那样,在整个表上设置一个background-color

    .table-striped > tbody {
        background-color: #fff;
    }
    

    【讨论】:

    • 其实你可以直接在tbody(.table-striped > tbody)上设置
    • @JonathanNicol 我删除了表格条纹,因为它并不是真正需要的,而且移动在这里比其他任何事情都重要。我现在也按照建议强制使用背景颜色。这是更新的链接,nasgame.apphb.com,你可以直接搜索 Matt Vincent。 jquery 和 css 是一样的。使用最新的 IOS 以及 Chrome 和 Safari,它根本不会保留该列!
    • 当我缩小浏览器大小时,它在桌面上的 Chrome 中工作,但它只在 ios 上没有保持固定。我有一个 Kindle Fire,它正确地固定在那里。
    • 我让一些人确认它在他们的 Android 手机上也能正常工作。
    • 是的,我明白你在 iOS 上的意思。抱歉,我没有在 iOS 上远程调试的工具。我试过 Edge Inspect,但只能检查表格,不能检查表格。
    【解决方案2】:

    具有固定列的响应式表格,无需使用任何 jQuery 或 javascript。

    • HTML

      <div class="table">
        <div class="table-scroll">
          <table class="table-main">
              <thead>
                  <tr>
                      <th class="fix-col">Name</th>
                      <th>Designation</th>
                      <th>Experience</th>
                      <th>Technology</th>
                      <th>Company</th>
                      <th>Location</th>
                      <th>Contact No.</th>
                      <th>Address</th>
      
                  </tr>
              </thead>
              <tbody>
                  <tr>
                      <td class="fix-col">Bob</td>
                      <td>Front End Developer</td>
                      <td>5 yrs</td>
                      <td>HTML,CSS</td>
                      <td>Google</td>
                      <td>California</td>
                      <td>9876543210</td>
                      <td>Google Office</td>
                  </tr>
                  <tr>
                      <td class="fix-col">Bob</td>
                      <td>Front End Developer</td>
                      <td>5 yrs</td>
                      <td>HTML,CSS</td>
                      <td>Google</td>
                      <td>California</td>
                      <td>9876543210</td>
                      <td>Google Office</td>
                  </tr>
                  <tr>
                      <td class="fix-col">Bob</td>
                      <td>Front End Developer</td>
                      <td>5 yrs</td>
                      <td>HTML,CSS</td>
                      <td>Google</td>
                      <td>California</td>
                      <td>9876543210</td>
                      <td>Google Office</td>
                  </tr>
                  <tr>
                      <td class="fix-col">Bob</td>
                      <td>Front End Developer</td>
                      <td>5 yrs</td>
                      <td>HTML,CSS</td>
                      <td>Google</td>
                      <td>California</td>
                      <td>9876543210</td>
                      <td>Google Office</td>
                  </tr>
                  <tr>
                      <td class="fix-col">Bob</td>
                      <td>Front End Developer</td>
                      <td>5 yrs</td>
                      <td>HTML,CSS</td>
                      <td>Google</td>
                      <td>California</td>
                      <td>9876543210</td>
                      <td>Google Office</td>
                  </tr>
              </tbody>
          </table>
      </div>
      

    • CSS

      .table-main {
          border: none;
          border-right: solid 1px rgb(75, 90, 102);
          border-collapse: separate;
          border-spacing: 0;
          font: normal 13px Arial, sans-serif;
      }
      
      .table-main thead th {
          background-color: rgb(203, 220, 233);
          border: none;
          color: #336B6B;
          padding: 10px;
          text-align: left;
          text-shadow: 1px 1px 1px #fff;
          white-space: nowrap;
      }
      
      .table-main tbody td {
          border-bottom: solid 1px rgb(75, 90, 102);
          color: #333;
          padding: 10px;
          text-shadow: 1px 1px 1px #fff;
          white-space: nowrap;
      }
      
      .table {
          position: relative;
      }
      
      .table-scroll {
          margin-left: 131px;
          overflow-x: scroll;
          overflow-y: visible;
          padding-bottom: 5px;
          width: 500px;
      }
      
      .table-main .fix-col {
          border-left: solid 1px rgb(75, 90, 102);
          border-right: solid 1px rgb(75, 90, 102);
          left: 0;
          position: absolute;
          top: auto;
          width: 110px;
      }
      

    这是 JSFiddle 链接:https://jsfiddle.net/mahendra_ahada/vqkxcn3s/1/

    【讨论】:

      【解决方案3】:
      .table-responsive>.fixed-column {
        position: fixed;
      }
      

      这个改变有更好的结果,但还不是很正确。

      【讨论】:

        猜你喜欢
        • 2019-07-06
        • 2013-11-13
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 2018-01-13
        • 1970-01-01
        • 1970-01-01
        • 2014-12-31
        相关资源
        最近更新 更多