I have an angular material table which I need to generate some somewhat repetitive columns for. So I made a component that generates the repeating columns and attempted to inject it into my mat-table
, but it does not appear to work. Am I missing something obvious? I've built tables with dynamically named columns before but I did it within the same component as the table itself, so I'm thinking it's an issue of being contained within another element or something.
I believe this is possible, just can't find the right terms to google. Tried to make the example as simple as I could. The 'property' attribute on the repetitive-columns
component helps to dynamically generate unique column names to feed back to the displayedColumns
property for the mat-table
The displayedColumns
value gets updated naively in this example and generates a ExpressionChangedAfterItHasBeenCheckedError, but appears to me like it should work otherwise:
Table Template
<table mat-table [dataSource]="ds">
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>SKU</th>
<td mat-cell *matCellDef="let c" class="text-uppercase">
{{ c.sku }}
</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>
<!-- Comment the following 2 components to see how the 'displayedColumns' changes, and the table works with both commented -->
<app-my-repetitive-columns title="Base" property="base"></app-my-repetitive-columns>
<app-my-repetitive-columns title="Committed" property="committed"></app-my-repetitive-columns>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true" sticky></tr>
<tr mat-row *matRowDef="let c; let i = dataIndex; columns: displayedColumns; let even = even"
[class.exclusions]="c.hasExclusions" [class.striped]="even"></tr>
</table>
"Repetitive Columns" Template
<!-- With a supplied input value of "base" for "property", the column name here should be 'base-sales' -->
<ng-container [matColumnDef]="columnNames[0]">
<th mat-header-cell *matHeaderCellDef>{{title}} Sales</th>
<td mat-cell *matCellDef="let c" class="text-uppercase">
{{ c[property].sale }}
</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>
<ng-container [matColumnDef]="columnNames[1]">
<th mat-header-cell *matHeaderCellDef>{{title}} Impact</th>
<td mat-cell *matCellDef="let c" class="text-uppercase">
{{ c[property].impact }}
</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>
<ng-container [matColumnDef]="columnNames[2]">
<th mat-header-cell *matHeaderCellDef>{{title}} Impact %</th>
<td mat-cell *matCellDef="let c" class="text-uppercase">
{{ c[property].impactPct }}
</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>