简体   繁体   中英

primeng data table columns width autofit based on contents

I am using prime ng data table in my angular application, I want to adjust width based on the content without giving fixed width it should adjust automatically. Can anyone help me with this? 在这张图片中,我想减小手机号码列的宽度以适应没有空格的内容

<p-table #dataGrid [columns]="columns" [value]="data" [paginator]="columns.length > 0 ? paginator : false"
  [rowsPerPageOptions]="rowsPerPageOptions" [totalRecords]="totalRecords" [first]="first"
  [reorderableColumns]="true" [resizableColumns]="true" [(rows)]="rows" [lazy]="lazy" [lazyLoadOnInit]="false"
  (onLazyLoad)="loadData($event)" [loading]="loading" selectionMode="single" (onRowSelect)="rowSelect($event)"
  (onColReorder)="saveStateToSession()" (onPage)="paginationEvent();" [sortOrder]="sortOrder" [ngClass]="{'disabledrow':disabledRow}"
  (onRowReorder)="updatePriority($event)">
<!---Some data---->
</p-table>

This will help you.

<p-table [resizableColumns]="true" [autoLayout]="true">

in case anyone is still struggling with this:

do use: [autoLayout]="true"

but remove: [resizeableColumns]="true" and [scrollable]="true"

the scrollable and resizable properties are not compatible with the autoLayout property, for technical reasons. per: AutoLayout not working in PrimeNG TurboTable

this will get you columns that are as wide as the content determines them to be.

The scrollable and resizable properties are not compatible with the autoLayout property, for technical reason.

 <p-table #dt [value]="records" [columns]="cols" responsiveLayout="scroll" dataKey="userid" styleClass="p-datatable-sm" [loading]=":loadingSpinner" [columns]="cols" [exportFilename]="exportName" [rows]="100" [(contextMenuSelection)]="selectedItem" scrollHeight="450px" [tableStyle]="{ width, 'max-content'}" [autoLayout]="true" columnResizeMode="expand" [contextMenu]="cm" (onEditComplete)="editGrid($event)" [rowsPerPageOptions]="[100, 200, 500]" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM