cssangularprimengprimeng-table

How to set different style & width of the column in frozen & unfrozen columns table in primeng


I am currently using PrimeNG table control with a frozen column feature.

Everything working fine and I am able to get frozen column but now I want to modify my frozen and unfrozen column styles and custom width of column for that I have used below property.

Code:

<p-table [columns]="scrollableCols" [frozenColumns]="frozenCols" [value]="cars" [scrollable]="true" scrollHeight="300px" frozenWidth="250px">
  <ng-template pTemplate="colgroup" let-columns>
    <colgroup>
      <col style="width:200px"> 
      <col style="width:50px">
      <col style="width:100px">
      <col style="width:100px"> 
      <col style="width:100px">
      <col style="width:100px">
    </colgroup>
  </ng-template>
  <ng-template pTemplate="header" let-columns>
    <tr>
      <th *ngFor="let col of columns">
        {{col.header}}
      </th>
    </tr>
  </ng-template>
  <ng-template pTemplate="body" let-rowData let-columns="columns">
    <tr>
      <td *ngFor="let col of columns">
        {{rowData[col.field]}}
      </td>
    </tr>
  </ng-template>
</p-table>

Output : enter image description here

by using width I am able to achieve width of my columns but for the first 2 columns, both froze & unfroze column table automatically taking the same width for all 2 columns I want to assign the different sizes of columns in both froze & unfroze column s.

is it possible to add different sizes for froze & unfroze columns?


Solution

  • All you need is pTemplate="frozencolgroup"

    <ng-template pTemplate="frozencolgroup" let-columns>
            <colgroup>
                <col style="width:200px">
                <col style="width:50px">
            </colgroup>
        </ng-template>
        <ng-template pTemplate="colgroup" let-columns>
            <colgroup>
                <col style="width:100px">
                <col style="width:100px">
                <col style="width:100px">
                <col style="width:100px">
            </colgroup>
        </ng-template>
    

    Demo here

    UPDATE: With scroll table can be broke row height. Below tip function to fix this

    makeRowsSameHeight() {
        setTimeout(() => {
          if ($('.ui-table-scrollable-wrapper').length) {
            let wrapper = $('.ui-table-scrollable-wrapper');
            wrapper.each(function () {
              let w = $(this);
              let frozen_rows: any = w.find('.ui-table-frozen-view tr');
              let unfrozen_rows = w.find('.ui-table-unfrozen-view tr');
              for (let i = 0; i < frozen_rows.length; i++) {
                if (frozen_rows.eq(i).height() > unfrozen_rows.eq(i).height()) {
                  unfrozen_rows.eq(i).height(frozen_rows.eq(i).height());
                } else if (frozen_rows.eq(i).height() < unfrozen_rows.eq(i).height()) {
                  frozen_rows.eq(i).height(unfrozen_rows.eq(i).height());
                }
              }
            });
          }
        });
      }
    

    UPDATE DEMO: https://stackblitz.com/edit/angular-primeng-table-frozen-columns-dpsm8l