2
votes

I have an issue with primeNG table. The table has many columns, on the column toggle, the column width is changing and the table looks like a mess.

The table Scrollable and Resizable, so according to documentation auto-layout is not supported due to technical reasons.

So maybe there is some workaround, or is there a way of preserving column size when user change it manually. Any help or idea .. see stackblitz project for repro

enter image description here https://stackblitz.com/edit/angular-primeng-width-rjgg7f?file=src/app/app.component.ts

    <p-table #dt [columns]="selectedColumns" [value]="carsData" [paginator]="true" [rows]="10"[scrollable]="true" scrollHeight="200px" [resizableColumns]="true">
        <ng-template pTemplate="caption">

            <div style="text-align:left">
                <p-multiSelect [options]="columns" [(ngModel)]="selectedColumns" optionLabel="header"
                    selectedItemsLabel="{0} columns selected" [style]="{minWidth: '200px'}"
                    defaultLabel="Choose Columns"></p-multiSelect>
            </div>

        </ng-template>
  <ng-template pTemplate="colgroup" let-columns>
                <colgroup>
                    <col *ngFor="let col of columns" [ngStyle]="{'width': col.width}">
                </colgroup>
            </ng-template>
        <ng-template pTemplate="header" let-columns>
            <tr>
         <th rowspan="2" style="width:10%;text-align: center" pResizableColumn>
            </th>
                <th *ngFor="let col of columns" pResizableColumn>
                    {{col.header}}
                </th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-rowData let-columns="columns" >
            <tr>
        <td (click)="onRowSelectAction(rowData)">
                <div id="btnwrap" style="width:100%">
                    <p-menu #menu [popup]="true" [model]="items" appendTo="body"></p-menu>
                    <button type="button" pButton icon="pi pi-ellipsis-h"
                        style="margin-left:2px;margin-top:2px;text-align: center" [label]="additionalButtonTitle"
                        (click)="menu.toggle($event)" class="btn btn-dark">
                    </button>

                </div>
            </td>
                <td *ngFor="let col of columns" class="ui-resizable-column" class="ui-resizable-column">
                    {{rowData[col.field]}}
                </td>
            </tr>
        </ng-template>
    </p-table>
2

2 Answers

2
votes

I found the problem, it's the first column that contains an action button. I was setting the column style in pTemplate="header" but it needs to be set in pTemplate="colgroup"

updated pTemplate="colgroup"

 <ng-template pTemplate="colgroup" let-columns>
            <colgroup>
               <col style="width:4%;text-align: center">
               <col *ngFor="let col of columns" [ngStyle]="{'width': col.width}">
     </colgroup>
   </ng-template>

updated pTemplate="header"

<ng-template pTemplate="header" let-columns>
            <tr>
         <th rowspan="2" pResizableColumn>
            </th>
                <th *ngFor="let col of columns" pResizableColumn>
                    {{col.header}}
                </th>
            </tr>
    </ng-template>
0
votes

I could not see that issue, it is working fine for me. Please check.

enter image description here

Look updated image, column width is coming only for 5 columns whereas 6 columns are showing.

enter image description here