【发布时间】:2020-11-08 07:39:39
【问题描述】:
我有以下 Angular Material mat-table 用于列出员工。我还有其他表格需要转换为相同的样式和结构。如何组件化此 Employee 表以供重用?使用像 Material 这样的组件库的重点是重用常用组件,我无法理解如何轻松重用此表。
<table mat-table matSort [dataSource]="employeeDisplayList" class="mat-elevation-z8 table">
<ng-container matColumnDef="selected">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let element; let i = index"
[ngClass]="{'dependent': element.relationship !== 'Primary'}">
<input type="checkbox" id="enrollEmployee-{{ i }}" name="enrollEmployee">
<label for="enrollEmployee-{{ i }}"></label>
</td>
</ng-container>
<!-- Employee Name Column -->
<ng-container matColumnDef="employeeName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Employee Name </th>
<td mat-cell *matCellDef="let element"
[ngClass]="{'dependent': element.relationship !== 'Primary'}"> {{element.employeeName}} </td>
</ng-container>
<!-- Relationship Column -->
<ng-container matColumnDef="relationship">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Relationship </th>
<td mat-cell *matCellDef="let element"
[ngClass]="{'dependent': element.relationship !== 'Primary'}"> {{element.relationship}} </td>
</ng-container>
<!-- Medical Column -->
<ng-container matColumnDef="medical">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Medical </th>
<td mat-cell *matCellDef="let element"
[ngClass]="{'dependent': element.relationship !== 'Primary'}"> {{element.medical}} </td>
</ng-container>
<!-- Plan Name Column -->
<ng-container matColumnDef="planName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Plan Name </th>
<td mat-cell *matCellDef="let element"
[ngClass]="{'dependent': element.relationship !== 'Primary'}"> {{element.planName}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
TS:
displayedColumns: string[] = ['selected', 'employeeName', 'relationship', 'medical', 'planName'];
@ViewChild(MatSort) sort: MatSort;
ngOnInit() {
this.employeeList = this.enrollmentService.getEmployees();
this.employeeDisplayList = new MatTableDataSource(this.employeeList);
this.employeeDisplayList.sort = this.sort;
}
【问题讨论】: