Sticky header not working with resizable column in Primeng.?
Asked Answered
T

5

15

I am trying to implement both column resize and stick header. But sticky header works fine if I won't use the column resize. If I implement both, column resize 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: 70px;
    }

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

and for the colum resize I used the below code, [resizableColumns]="true", pResizableColumn

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

If I remove the resizbleColumns and pResizableColumn sticky header works fine. How can I make it works both things.? Here is the stackblitz and Demo

Turbulent answered 3/9, 2019 at 8:53 Comment(0)
F
16

when you set the p-table columns to be resizable the add a class ui-table-resizable this will reset some css property one of its the position of th to relative so you will lose sticky future

this should fix the problem

:host ::ng-deep .ui-table .ui-table-thead > tr > th {
  position: -webkit-sticky;
  position: sticky;
  background: blue;
  color: white;
  top: 0px;
  z-index: 1;
}

:host ::ng-deep .ui-table-resizable > .ui-table-wrapper {
  overflow-x: initial !important;
}

:host ::ng-deep .ui-table-resizable .ui-resizable-column {
  position: sticky !important;
}

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

demo

Updated!

add the style in the component decorator is not reusable ,base of primeng theme recommendation of creating custom style we can do like this

style.scss

.sticky-table {

      &.ui-table .ui-table-thead > tr > th {
        position: -webkit-sticky;
        position: sticky !important;
        background: blue;
        color: white;
        top: 0px;
        z-index: 1;
      }

     &.ui-table-resizable > .ui-table-wrapper {
        overflow-x: initial !important;
      }

      &.ui-table-resizable .ui-resizable-column {
        position: sticky !important;
      }

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

template

<p-table styleClass="sticky-table" [columns]="cols" [value]="cars [resizableColumns]="true">
....
</p-table>

demo

Fi answered 5/9, 2019 at 16:13 Comment(0)
E
6

Anyone who still might be interested:
For me it worked by just adding the following

:host ::ng-deep .p-datatable .p-datatable-wrapper {
    overflow-x: initial;
}

The Resizable-Fatures adds "overflow-x: auto" to the table, which hides the stick header.

Extinctive answered 28/12, 2020 at 23:56 Comment(2)
It works for autoLayout="true". Thanks.Vocal
It also works for responsiveLayout="scroll"Godhood
B
1
:host ::ng-deep .p-datatable .p-datatable-thead > tr > th {
        position: -webkit-sticky;
        position: sticky;
        background: blue;
        color: white;
        top: 0px;
        z-index: 1;
      }
      
      :host ::ng-deep .p-datatable-resizable > .p-datatable-wrapper {
        overflow-x: initial !important;
      }
      
      :host ::ng-deep .p-datatable-resizable .ui-resizable-column {
        position: sticky !important;
      }
      
      @media screen and (max-width: 64em) {
        :host ::ng-deep .p-datatable .p-datatable-thead > tr > th {
          top: 0px;
        }
      }
Bixby answered 29/3, 2021 at 19:18 Comment(0)
A
0

Please use the below code:

<p-table [columns]="cols" [value]="rows" [autoLayout]="true">
Alundum answered 18/5, 2021 at 18:17 Comment(1)
Explanation would be helpful :-)Turbulent
R
0

I had sticky Table Headers working without resize before. After adding resize it didn't stick anymore.

I fixed it on my small datatable (p-datatable-sm). I guess this works for normal size datatables with the right css class aswell.

Angular v15.1, primeng 15.2

The Fix for me:

:host ::ng-deep .p-datatable-sm .p-resizable-column {
    position: sticky !important;
}

Explanation:

.p-datatable-resizable-table>.p-datatable-thead>tr>th.p-resizable-column:not(.p-frozen-column)

adds:

position:relative 

and

:host ::ng-deep .p-datatable-sm .p-resizable-column {
    position: sticky !important;
}

overwrites that.

Resignation answered 27/2, 2023 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.