Trying to use the mat-table
directives, such as *matRowDef
and multiTemplateDataRows
to build a table where each row can have a varying number of sub-rows.
An example interface would look like this:
interface ReportCard {
student: string;
reports: Report[];
}
interface Report {
class: string;
teacher: string;
grade: string;
}
...where there is no fixed number of reports.
I can easily build a table with *ngFor
, but I would like to use the built-in Material Table directives.
<table>
<thead>
<tr>
<th>student</th>
<th colspan="3">
classes
</th>
</tr>
<tr>
<th></th>
<th>name</th>
<th>teacher</th>
<th>grade</th>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let reportCard of reportCards">
<tr
*ngFor="let report of reportCard.reports; let first = first"
>
<td
*ngIf="first"
[attr.rowspan]="reportCard.reports.length"
>
{{ reportCard.student }}
</td>
<td>{{ report.class }}</td>
<td>{{ report.teacher }}</td>
<td>{{ report.grade }}</td>
</tr>
</ng-container>
</tbody>
</table>
I think I can also create an arbitrarily high number of templates for data rows using multiTemplateDataRows
to handle each report (similar to how it is done in this answer), but that does not seem like the best solution. Also I'm not sure I am able to access the correct index for each report while trying to populate the <td>
cell with the correct data.
Stackblitz of table I am trying to build
Any help appreciated.