如何让Vue b表用固定标题滚动主体

Abi*_*n47 5 html css vue.js vuejs2 bootstrap-vue

b-table在页面上有一个元素,当前显示数据库中的一堆数据.目前它是分页的,但反馈表明他们宁愿在一个滚动视图中显示所有信息.我可以做到这一点,但问题是如果我设置一个包含div滚动整个表,它也会滚动列标题.我需要找到一种方法,只需滚动表体,同时保留列标题,我宁愿能够在元素本身的范围内进行,而不是使用完全独立的标题绑定一些东西和一堆Javascript索具在背景中的身体.

bootstrap-vue表组件的组件引用下,它表示有一个属性被调用tbody-class,看起来它是一种为tbody指定自定义类的方法(疯狂到足够).但是,该页面没有说明如何使用它,我的实验没有产生任何结果:

<b-table 
    tbody-class="my-class"   <- Applies prop to table but not to tbody
    :tbody-class="my-class"  <- Seemingly ignored entirely
>
Run Code Online (Sandbox Code Playgroud)

听起来这个问题在这个问题线程上得到了解决,但它并没有详细说明它是如何解决的.它提到该功能已添加到"下一次更新"中,但该注释后发布的版本的补丁说明和文档都没有提到添加(除非它表示我在前一段中提到的属性).它确实讨论了使用CSS选择器以迂回方式应用样式,但我也无法使用它.(在以下示例中,tbodyChrome检查器中没有应用的样式.)

.table.my-table > tbody {
    height: 100px;
}
Run Code Online (Sandbox Code Playgroud)

我使用的Vue版本是2.2.6.

jtl*_*sey 1

我需要找到一种方法,能够仅滚动表格主体,同时将列标题保留在适当的位置,并且我希望能够在元素本身的范围内完成此操作,而不是使用完全独立的标题来安装某些内容和身体在后台有一堆 Javascript 装备

我为您制作了一个示例存储库,以了解如何使其工作(也有隐藏的滚动条)。

可能还有其他方法可以完成同样的事情,但这就是我所做的。

<!-- wrap table in container with class. -->
    <b-container fluid class="table-container">
      <b-table
        :items="items" 
        :fields="fields" 
        caption-top
      >
      </b-table>
    </b-container>
Run Code Online (Sandbox Code Playgroud)

这是带有隐藏滚动条的 CSS。

.table-container {
     height: calc(100vh - 450px);
}
 .table-container table {
     display: flex;
     flex-flow: column;
     height: 100%;
     width: 100%;
}
 .table-container table thead {
     flex: 0 0 auto;
     width: 100%;
}
 .table-container table tbody {
     flex: 1 1 auto;
     display: block;
     width: 100%;
     overflow-y: auto;
}
 .table-container table tbody tr {
     width: 100%;
}
 .table-container table thead, .table-container table tbody tr {
     display: table;
     table-layout: fixed;
}
 .table-container table {
     border-collapse: collapse;
}
 .table-container table td, .table-container table th {
     padding: 0.4em;
}
 .table-container table th {
     border: 1px solid black;
     font-size: 0.7vw;
}
 .table-container table td {
     border: 1px solid #e7e1e1;
     font-size: 0.85em;
    /* necessary to set for proper "showing row x of y" calculations if infinate scoll */
     white-space: nowrap;
     text-align: center;
     padding: 10px 5px;
     white-space: nowrap;
     text-overflow: ellipsis;
}
 .table-container table thead {
     border: 2px solid #0F0FA3;
}
 .table-container th {
     background-color: #0F0FA3;
     color: #b5aba4;
     cursor: pointer;
     -webkit-user-select: none;
   -moz-user-select: none !important;
     -ms-user-select: none;
     user-select: none;
}
 .table-container table tbody td {
     padding: 8px;
     cursor: pointer;
}
 .table-container table tbody tr:hover {
     background-color: rgba(168, 168, 239, .5);
}
 .table-container table thead td {
     padding: 10px 5px;
}
 .table-container tr:nth-child(even) {
     background-color: rgba(168, 168, 239, 1);
}

/* START Adjustments for width and scrollbar hidden */
 .table-container th.table-action, .table-container td.table-action {
     width: 5.8vw;
}
 .table-container table thead {
     width: calc(100% - 1px);
}
 .table-container table tbody::-webkit-scrollbar {
     width: 1px;
}
 .table-container table tbody::-webkit-scrollbar {
     width: 1px;
}
 .table-container table tbody::-webkit-scrollbar-thumb {
     width: 1px;
}
/* END Adjustments for width and scrollbar */

Run Code Online (Sandbox Code Playgroud)

更新,另一种拥有固定标头但非常受限的方法(参考):

有几种方法可以做到这一点......并且不能总是保证跨浏览器:

创建一个类,例如 my-table-scroll

table.my-table-scroll,
table.my-table-scroll > thead,
table.my-table-scroll > tbody,
table.my-table-scroll > tfoot,
table.my-table-scroll > tbody > tr,
table.my-table-scroll > thead > tr {
  width: 100%;
  display: block
}
table.my-table-scroll > thead,
table.my-table-scroll > tbody,
table.my-table-scroll > tfoot {
  display: block;
  width: 100%;
  overflow-y: scroll;
}
table.my-table-scroll > thead,
table.my-table-scroll > tfoot {
  height: auto;
}
/* these last two rules you will need to tweak on an as needed basis */
table.my-table-scroll > tbody {
  /* set max height for tbody before it scrolls */
  max-height: 200px;
}
table.my-table-scroll > thead > tr > th,
table.my-table-scroll > thead > tr > td,
table.my-table-scroll > tbody > tr > th,
table.my-table-scroll > tbody > tr > td,
table.my-table-scroll > tfoot > tr > th,
table.my-table-scroll > tfoot > tr > td {
  display: inline-block;
  /* you need to explicitly specify the width of the table cells */
  /* either equal, or specify individual widths based on col index. */
  /* Here, we assume you have 4 columns of data */
  width: 25%;
}
/*
  could use sibling selectors to select specific columns for widths:
  td + td (second column)
  td + td + td (3rd column, etc)
*/
Run Code Online (Sandbox Code Playgroud)

然后将类放到您的 b 桌子上。