这是 Angular 12 和 @angular/material 12 对我有用的方法。此代码基于https://github.com/angular/components/issues/5889中的代码片段
@Component({
selector: 'app-column-template',
template: `
<ng-container matColumnDef>
<th mat-header-cell *matHeaderCellDef>{{ label || capitalize(name) }}</th>
<td mat-cell *matCellDef="let row">
<ng-container *ngTemplateOutlet="cellTemplate; context: { $implicit: row }"></ng-container>
</td>
</ng-container>
`,
// eslint-disable-next-line @angular-eslint/no-host-metadata-property
host: {
class: 'column-template cdk-visually-hidden',
'[attr.ariaHidden]': 'true',
},
})
export class ColumnTemplateComponent implements OnDestroy, OnInit {
@Input() name = '';
@Input() label: string | null = null;
@Input() align: 'before' | 'after' = 'before';
constructor(@Optional() public table: MatTable<unknown>) {}
@ViewChild(MatColumnDef, { static: true }) columnDef!: MatColumnDef;
@ViewChild(MatCellDef, { static: true }) cellDef!: MatCellDef;
@ViewChild(MatHeaderCellDef, { static: true }) headerCellDef!: MatHeaderCellDef;
@ViewChild(MatFooterCellDef, { static: true }) footerCellDef!: MatFooterCellDef;
@ContentChild('cell', { static: false })
cellTemplate: TemplateRef<unknown> | null = null;
ngOnInit(): void {
if (this.table && this.columnDef) {
this.columnDef.name = this.name;
this.columnDef.cell = this.cellDef;
this.columnDef.headerCell = this.headerCellDef;
this.columnDef.footerCell = this.footerCellDef;
this.table.addColumnDef(this.columnDef);
}
}
ngOnDestroy(): void {
if (this.table) {
this.table.removeColumnDef(this.columnDef);
}
}
capitalize(value: string): string {
return value.charAt(0).toUpperCase() + value.slice(1);
}
}
export type CellValueNeededFn = (data: Record<string, unknown>, name: string) => string;
@Component({
selector: 'app-column',
template: `
<ng-container matColumnDef>
<th mat-header-cell *matHeaderCellDef>{{ label || capitalize(name) }}</th>
<td mat-cell *matCellDef="let row">{{ getCellValue(row) }}</td>
</ng-container>
`,
// eslint-disable-next-line @angular-eslint/no-host-metadata-property
host: {
class: 'column cdk-visually-hidden',
'[attr.ariaHidden]': 'true',
},
})
export class ColumnComponent implements OnDestroy, OnInit {
@Input() name = '';
@Input() label: string | null = null;
@Input() align: 'before' | 'after' = 'before';
@Input() cellValueNeeded: CellValueNeededFn | null = null;
constructor(@Optional() public table: MatTable<unknown>) {}
@ViewChild(MatColumnDef, { static: true }) columnDef!: MatColumnDef;
@ViewChild(MatCellDef, { static: true }) cellDef!: MatCellDef;
@ViewChild(MatHeaderCellDef, { static: true }) headerCellDef!: MatHeaderCellDef;
@ViewChild(MatFooterCellDef, { static: true }) footerCellDef!: MatFooterCellDef;
@ContentChild('cell', { static: false })
cellTemplate: TemplateRef<unknown> | null = null;
ngOnInit(): void {
if (this.table && this.columnDef) {
this.columnDef.name = this.name;
this.columnDef.cell = this.cellDef;
this.columnDef.headerCell = this.headerCellDef;
this.columnDef.footerCell = this.footerCellDef;
this.table.addColumnDef(this.columnDef);
}
}
ngOnDestroy(): void {
if (this.table) {
this.table.removeColumnDef(this.columnDef);
}
}
capitalize(value: string): string {
return value.charAt(0).toUpperCase() + value.slice(1);
}
getCellValue(row: Record<string, unknown>): unknown {
return this.cellValueNeeded ? this.cellValueNeeded(row, this.name) : row[this.name];
}
}
ColumnComponent
对我来说建立在基础上的尝试ColumnTemplateComponent
最终以熟悉的方式结束
Error: Could not find column with id "...".
at getTableUnknownColumnError (table.js:1078) [angular]
blah-blah-blah...