table 上的水平滚动条带有粘性 header

Horizontal scrollbar on table with sticky header

我试图在 table 上使用粘性 header 实现水平滚动条,问题是当我实现粘性时滚动条没有出现。它只能通过触控板滚动,不能通过鼠标滚动。我正在为 angular

使用 ng2 smart table
 <ng2-smart-table
                      *ngIf="showMainTable"
                      [settings]="settings"
                      [source]="localDataSource"
                      (edit)="rowEdited($event, 'edit')"
                      (editConfirm)="rowEdited($event, 'editConfirm')"
                    ></ng2-smart-table>

样式

:host /deep/ ng2-smart-table table tr td:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background: white !important;
  // box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);

  border-left: none !important;
  border-right: none !important;
  border-top: none !important;
  border-bottom: solid 1px #f4f2f0 !important;

  box-shadow: 10px 0 10px -2px rgb(245, 241, 241) !important;
}

:host /deep/ ng2-smart-table table tr th:first-child {
  position: sticky;
  left: 0;
  width: 100px;
  z-index: 2;
  background: white !important;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}

:host /deep/ ng2-smart-table table tr th:first-child {
  z-index: 2;
  background: white !important;
}

:host /deep/ ng2-smart-table table thead tr {
  position: sticky;
  white-space: nowrap;
  padding: 10px 40px !important;
  top: 0;
  background: white !important;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}

:host /deep/ ng2-smart-table table thead th {
  position: sticky;
  top: 0;
  background: white !important;
  border: none;
  color: #ccc;
  cursor: pointer;
  white-space: nowrap;
  padding: 0;
  height: 56px;
  padding-left: 56px;
  vertical-align: middle;
  outline: none !important;
  color: rgba(0, 0, 0, 0.54);
  font-size: 12px;
  font-weight: 500;
}

:host /deep/ ng2-smart-table table tr:nth-child(1) th {
  // display: inline-flex !important;
  white-space: nowrap;
  padding: 10px 40px !important;
}

:host /deep/ .ng2-smart-sort-link {
  // display: inline-flex !important;
  color: rgba(0, 0, 0, 0.54) !important;
  font-size: 12px !important;
  font-weight: 500 !important;
  text-transform: uppercase;
}

:host /deep/ ng2-smart-table table tbody {
  // overflow: hidden;
}

:host /deep/ ng2-smart-table table {
  border: none;
}

:host /deep/ ng2-smart-table table tr td {
  // display: inline-flex !important;
  text-align: center !important;
  height: 48px;
  padding: 0 0 0 56px;
  height: 48px;
  font-size: 1.2rem;
  font-weight: 300;
  color: #111;
  white-space: nowrap;
  // overflow: hidden;
  text-overflow: ellipsis;
  border-left: none !important;
  border-right: none !important;
  border-top: none !important;
  border-bottom: solid 1px #f4f2f0 !important;
}

host 和 deep 是 angular 选择器,它将样式应用到 table 生成的 我将 table 包装在一个容器中,我能够实现滚动功能,但 table 失去了粘性。我希望 header 是粘性的,而 table 在底部有一个固定的水平滚动条,它总是可见的。

我把 table 包裹在一个容器里

<div
                    class="table-flow"
                    [ngClass]="{ 'disable-cover': mainTableLoading }"
                  >
                    <ng2-smart-table
                      *ngIf="showMainTable"
                      [settings]="settings"
                      [source]="localDataSource"
                      (edit)="rowEdited($event, 'edit')"
                      (editConfirm)="rowEdited($event, 'editConfirm')"
                    ></ng2-smart-table>
                  </div>

风格

.table-flow {
  overflow-x: scroll;
}

它显示滚动条,但我丢失了粘性 headers

Codesandbox Link https://codesandbox.io/s/elastic-satoshi-7ycxm

您的 table-flow 至少需要 position:relative 并且您需要将他的最大宽度设置为 100%,然后设置 overflow: auto :)

我已经通过为容器提供固定的最大高度解决了这个问题,这样它就会有粘性 header 和滚动。

.table-flow {
  max-height: 555px;
  overflow-x: scroll;
  max-width: auto;
  position: relative;
}

HTML

<div class="table-container">
  <ng2-smart-table
    [settings]="settings"
    [source]="source"
  ></ng2-smart-table>
</div>

CSS

.table-container {
  height: 600px;
  overflow-y: auto;
  border-top: 1px solid #dee2e6;
}    

::ng-deep table tr:first-child th {
  position: sticky !important;
  top: -1px;
  z-index: 100;
  background: white;
  border: 1px solid #dee2e6;
  height: 58px;
}