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?
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.
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 } ];
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>
You can do it by adding width="100%" in your <p-table>
tag. And then you can define width percentage to each dynamic column.
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>`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With