I am trying to make a mat-table where the column width automatically adjusts based on the content of the longest column.
<mat-table #table [dataSource]="dataSource">
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef class="myHeader"> No. </mat-header-cell>
<mat-cell *matCellDef="let element" class="myCell"> {{element.email}} </mat-cell>
</ng-container>
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef class="myHeader"> Name </mat-header-cell>
<mat-cell *matCellDef="let element" class="myCell"> {{element.name}} </mat-cell>
</ng-container>
</mat-table>
Example Data:
const DATA: Data[] = [
{email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="076a666e6b36476a666e6b2964686a">[email protected]</a>', name: 'Long name for this person'},
{email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83eee2eaefb1c3eee2eaefade0ecee">[email protected]</a>': 'name2',
{email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ce0e3e2ebe1ede5e0ede8e8fee9ffcce0e3e2ebe1ede5e0ede8e8fee9ffffa2efe3e1">[email protected]</a>: 'name3'}
];
The cell width needs to accommodate "[email protected]" and "Long name for this person".
I have tried using FxFlexFill and fxFlex in my CSS, but it didn't work.
.myHeader, .myCell{
flex: 0 0 100px;
white-space: nowrap;
}
Can anyone offer some help?