我尝试为具有多个排序的角度材料表创建一个包装器。因此,我喜欢为默认的table编写一个包装器组件,这是我的模板:
<div>
<table mat-table [dataSource]="tableData.dataSource" matMultiSort (matSortChange)="_emitParentEvent">
<ng-content></ng-content>
<tr mat-header-row *matHeaderRowDef="tableData.displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: tableData.displayedColumns;"></tr>
</table>
<mat-paginator [pageSize]="tableData.pageSize" [pageIndex]="tableData.pageIndex"
[pageSizeOptions]="tableData.pageSizeOptions" [length]="tableData.size ? tableData.size : 0"
(page)="tableData.onPagnationEvent($event)">
</mat-paginator>
</div>下面是组件的代码:
@Component({
selector: 'mat-multi-sort-table',
templateUrl: './mat-multi-sort-table.component.html',
styleUrls: ['./mat-multi-sort-table.component.scss']
})
export class MatMultiSortTableComponent implements OnInit {
@Input() tableData: TableData<any>;
@Output() matMulitSortChange: EventEmitter<MutliSortItem[]> = new EventEmitter<MutliSortItem[]>();
@ViewChild(MatMultiSort, { static: false }) sort: MatMultiSort;
constructor() { }
ngOnInit() {
this.tableData.dataSource.sort = this.sort;
}
_emitParentEvent() {
const tmpItmes = [];
for (let i = 0;*emphasized text* i < this.sort.actives.length; i++) {
tmpItmes.push(new MutliSortItem(this.sort.actives[i], this.sort.directions[i]));
}
this.matMulitSortChange.emit(Object.assign([], tmpItmes));
}
}当我将一些内容传递给我的组件时,不会,如下所示;
<mat-multi-sort-table [tableData]="table" (matMulitSortChange)="console.log($event)">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-multi-sort-header="id"> ID </th>
<td mat-cell *matCellDef="let row"> {{row.id}} </td>
</ng-container>
<ng-container matColumnDef="progress">
<th mat-header-cell *matHeaderCellDef mat-multi-sort-header="progress"> Progress </th>
<td mat-cell *matCellDef="let row"> {{row.progress}}% </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-multi-sort-header="name"> Name </th>
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<ng-container matColumnDef="color">
<th mat-header-cell *matHeaderCellDef mat-multi-sort-header="color"> Color </th>
<td mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.color}} </td>
</ng-container>
<ng-container matColumnDef="date">
<th mat-header-cell *matHeaderCellDef mat-multi-sort-header="date"> Date </th>
<td mat-cell *matCellDef="let row"> {{row.date | date:'YYYY-MM-DD HH:mm'}} </td>
</ng-container>
</mat-multi-sort-table>我得到以下错误:ERROR Error: "Could not find column with id "id"我发现,出现这个问题是因为表在初始化时需要它的定义,而ng-content并非如此。是否有解决此问题的方法?
发布于 2020-04-20 04:08:54
您有一个关于角度材质github的示例:https://github.com/angular/components/tree/master/src/components-examples/material/table/table-wrapped。
你必须这样做:
export class WrapperTable<T> implements AfterContentInit {
@ContentChildren(MatHeaderRowDef) headerRowDefs: QueryList<MatHeaderRowDef>;
@ContentChildren(MatRowDef) rowDefs: QueryList<MatRowDef<T>>;
@ContentChildren(MatColumnDef) columnDefs: QueryList<MatColumnDef>;
@ViewChild(MatTable, {static: true}) table: MatTable<T>;
ngAfterContentInit() {
this.columnDefs.forEach(columnDef => this.table.addColumnDef(columnDef));
this.rowDefs.forEach(rowDef => this.table.addRowDef(rowDef));
this.headerRowDefs.forEach(headerRowDef => this.table.addHeaderRowDef(headerRowDef));
}
}https://stackoverflow.com/questions/59088912
复制相似问题