Search code examples
htmlcssangularprimengprimeng-datatable

Sticky header of p-table not working with [scrollable] = true in Primeng.?


I am trying to implement both [scrollable]="true" and stick header in p-table of PrimeNg. But sticky header works fine if I won't use the scrollable. If I implement both, scrollable is working but sticky header is not working.

I used the following css from primeng for the sticky header.

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

and for scrollable I used the below code, [scrollable]="true"

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

If I remove [scrollable]="true" sticky header works fine. How can I make it works both things.? Here is the stackblitz.


Solution

  • The structure in the scrollable table is different. so you should give the sticky style to this ancestor element instead:

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

    See it live on stackblitz


    Minimal example to describe the issue:

    the below sticky header is not working because we added sticky to the wrong element. to fix it we should add the sticky to .header instead:

    <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