Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the width of a column be altered in a sortable primeng table?

I want to reduce the width of a few columns I'm using in primeng grid. However as per my understanding, we can only change the width of columns we create using "p-column" or the "th" tag.

PFA code snippets below: HTML:

 <th *ngFor="let col of columns" [pSortableColumn]="col.field"colspan="col.colspan" style="text-align: 
center;">
                {{col.header}}
                <p-sortIcon [field]="col.field"></p-sortIcon>
      </th>

And the TS:

this.cols = [
                { field: 'first', header:'FIRST'},
                { field: 'second', header: 'SECOND'},
                { field: 'third', header: 'THIRD'},
                { field: 'fourth', header: 'FOURTH'},
                { field: 'fifth', header: 'FIFTH'},
                { field: 'sixth', header: 'SIXTH'}
            ];

How can we change the width for a dynamic column in sortable primeng table?

like image 359
Vaibhav Tiwari Avatar asked Jun 25 '18 07:06

Vaibhav Tiwari


People also ask

How do you set the width of a Primeng table?

if you click on the change tab and add a row the width is getting change. Ah, I see. It looks like the second column's width is also being set (to 200px). If you remove that setting and allow that column to take up the remaining width of the table, the first column's width setting will be honored.

How do I turn off sort in Primeng table?

Bookmark this question. Show activity on this post. cols = [ { field: 'name', header: 'Name', sort: true }, { field: 'age', header: 'Age', sort: true }, { field: 'gender', header: 'Gender', sort: false }, { field: 'status', header: 'Status', sort: false } ];


3 Answers

updated TS file as, Pass the required width value similar to that of header name for the dynamic columns:

this.cols = [
{field: 'first', header: 'FIRST', width: '20%'},
{field: 'second', header: 'SECOND', width: '30%'},
{field: 'third', header: 'SECOND', width: '50%'}]

Use ngStyle attribute to bind the value of width:

eg:

<th *ngFor="let col of columns" [pSortableColumn]="col.field" colspan="col.colspan" 
       [ngStyle]="{'width': col.width}">
                {{col.header}}
       <p-sortIcon [field]="col.field"></p-sortIcon>
</th>
like image 52
Vaibhav Tiwari Avatar answered Oct 06 '22 01:10

Vaibhav Tiwari


You can do it by adding width="100%" in your <p-table> tag. And then you can define width percentage to each dynamic column.

like image 39
Siddharth Shah Avatar answered Oct 06 '22 03:10

Siddharth Shah


If you are using style below, add colgroup to define style for column.

`     <p-table ...>
        <ng-template pTemplate="colgroup" let-columns>
        <colgroup>
            <col *ngFor="let col of columns" style="width: 40px;">
        </colgroup>
        </ng-template>
       </p-table>`
like image 45
Feng Zhang Avatar answered Oct 06 '22 03:10

Feng Zhang