【问题标题】:bootstrap table responsive head fixed body scrollable引导表响应式头部固定体可滚动
【发布时间】:2017-02-22 23:21:33
【问题描述】:

我正在使用 bootstrap 3 使我的网站具有响应性,并且我有一个带有“表格响应”的 div 的表格。我想要一个允许我在屏幕很小时垂直滚动的表格,但它也需要水平滚动,因为它们在表格中有超过 50 个项目。如何使表格可滚动且响应迅速?

我正在尝试这个 css,但表格会增长并且没有水平滚动......

application.css.scss

  //table becomes responsive with scrollbar
  .table-responsive {
   width: 100%;
   height: 200px !important;
   overflow: auto;
   -webkit-overflow-scrolling: touch;
   -ms-overflow-style: -ms-autohiding-scrollbar;
   border: 1px solid #DDD;
  }

上面的css可以工作,但是头部需要固定在滚动体上方。

html

<div class= "container">
 <div class="table-responsive">
  <table class="table table-colored table-hover table-bordered">
   <thead>
    <tr>
      <th width="20%">Applicant's Name</th>
      <th width="20%">Applicant's Email</th>
      <th width="20%">Date Requested </th>
      <th width="40%">Report View/Download</th>
    </tr>
   </thead>
  <tbody>
        <% current_manager.reportapprovals.each do |ra| %>
          <% if ra.report.present? %>
              <tr>
                <td width="20%"><%= ra.tenant_last_name.truncate(17) %></td>
                <td width="20%"><%= ra.tenant_email.truncate(17) %></td>
                <td width="20%"><%= ra.date_approved %></td>
              </tr>
          <% end %>
        <% end %>
      </div>
      <% end %>
  </tbody>
</div>
</table>

【问题讨论】:

标签: html css twitter-bootstrap


【解决方案1】:

您可以像这样更改 CSS 以获得垂直和水平滚动

.table-responsive {
   width: 100%;
   height: 100%;
   margin-bottom: 15px;
   overflow-x: auto;
   overflow-y: auto;
   display: block;
   -webkit-overflow-scrolling: touch;
   -ms-overflow-style: -ms-autohiding-scrollbar;
   border: 1px solid #DDD;
}

https://jsfiddle.net/xx8j9tLt/

更新

带有固定标题

CSS

.container {
  width:400px;
  height: 100px;
}
.table-responsive {
    width: 100%;
    height: 100%;
    margin-bottom: 15px;
    overflow-x: auto;
    overflow-y: auto;
    display: block;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: -ms-autohiding-scrollbar;
    border: 1px solid #DDD;
    position: relative;
}

table thead {
   position: fixed;
   width: auto;
   overflow: auto;
}

JS(带有 jquery)

function setThead() {
    var tableWidth = $('.table-responsive').width();
  $('.table-responsive thead').css('width',tableWidth);
};
setThead();
window.onresize = setThead;

https://jsfiddle.net/xx8j9tLt/1/

【讨论】:

  • 这在滚动时没有固定的标题
猜你喜欢
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多