【问题标题】:Horizontal scrollbar on table with sticky header带有粘性标题的表格上的水平滚动条
【发布时间】:2020-04-11 19:01:53
【问题描述】:

我试图在带有粘性标题的表格上实现水平滚动条,问题是当我实现粘性时滚动条没有出现。它只能通过触控板而不是鼠标滚动。我使用 ng2 智能表进行角度分析

 <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 是角度选择器,它将样式应用于由它生成的表格 我将表格包裹在一个容器中,我能够实现滚动功能,但表格失去了粘性。我希望标题是粘性的,并且表格在底部有一个固定的水平滚动条,它总是可见的。

我将桌子包裹在一个容器中

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

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

代码沙盒链接https://codesandbox.io/s/elastic-satoshi-7ycxm

【问题讨论】:

    标签: html css sticky


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        您的表格流至少需要定位:相对,您需要将他的最大宽度设置为 100%,然后设置溢出:自动 :)

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 2018-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多