在Primeng中,p表的粘贴标头无法与[scrollable] = true一起使用

问题描述

我正在尝试在PrimeNg的p表中同时实现[scrollable] =“ true”和stick标头。但是,如果我不使用可滚动的标题,则粘性标头效果很好。如果同时实现这两种功能,则scrollable起作用,但粘性标头不起作用。

我使用了primeng的以下CSS作为粘性标头。

 :host ::ng-deep .ui-table .ui-table-thead > tr > th {
        position: -webkit-sticky;
        position: sticky;
        top: 69px;
        Box-shadow: 1px 3px 6px 0 rgba(32,33,36,0.10);
    }

    @media screen and (max-width: 64em) {
        :host ::ng-deep .ui-table .ui-table-thead > tr > th {
            top: 99px;
        }
    }

对于可滚动,我使用了以下代码[scrollable]="true"

<p-table [columns]="cols" [value]="cars1" [scrollabe]="true">
...
 <th *ngFor="let col of columns" >

如果我删除[scrollable]="true",则粘性标头可以正常工作。我怎样才能使它同时起作用呢? 这是stackblitz

解决方法

可滚动表中的结构不同。因此您应该将sticky样式赋予此祖先元素:

:host ::ng-deep .ui-table-scrollable-header{
  position: sticky;
  position: -webkit-sticky;
  top: 0;
  z-index: 1000;
}

See it live on stackblitz


描述问题的最小示例:

下面的sticky标头不起作用,因为我们在错误的元素上添加了粘性。要解决此问题,我们应该将sticky添加到.header

<div style="height: 1500px; background: #def;">
  <div class="header" style="background: #fed;"><!-- <- instead add sticky to here -->
    <div style="position: sticky;top: 0;">header</div> <!-- <-- not here -->
  </div>
  <div class="body" style="background: blue; height: 1500px;">
    <div>body</div>
  </div>
</div>

minimal example buggy version | minimal example fixed version