不要在.mat-header-cell 或.mat-cell 上设置宽度,而是在粘性列上设置width 和left (.mat-column-<label>)。为了让它工作,左边的值需要至少设置为前一列的宽度:
.mat-column-name {
width: 110px;
}
.mat-column-position {
width: 80px;
left: 110px !important;
}
注意:您需要使用!important,因为left 直接设置在.mat-header-cell 和.mat-cell 上。
如果您想更动态地设置粘性列的样式,可以将样式添加到列定义中,然后在模板中使用:
const DEFINITIONS: ColumnDefintion[] = [
{label: 'name', sticky: true, style: {'width':'100px', 'left':'0px'}},
{label: 'position', sticky: true, style: {'width':'80px', 'left':'100px'}},
{label: 'weight'},
{label: 'symbol'},
]
<!-- ... -->
<ng-container *ngIf="colDef.sticky" [matColumnDef]="colDef.label" sticky>
<th mat-header-cell *matHeaderCellDef [ngStyle]="colDef.style">{{colDef.label}}</th>
<td mat-cell *matCellDef="let element" [ngStyle]="colDef.style">{{element[colDef.label]}}</td>
</ng-container>
<!-- ... -->
工作示例:Stackblitz