I have came across a situation where I need to implement logic for displaying nested angular mat table. I already have implemented logic for displaying nested (inner) table with data source of child element, But situation is tricky when there is third level child items which again can have nested elements. the example data can be seen below.
So far I have fetched data for parent data source and child data source and displayed using mat table but unable to define logic for generating child mat table at run time based on data receives from backend service (On particular row item click). Current implantation is as follow (this is for nested mat table for level 1):
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
<div class="example-element-detail" *ngIf="element._mdbpartchilds?.length" [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
<div class="inner-table mat-elevation-z8" *ngIf="expandedElement">
<table #innerTables mat-table #innerSort="matSort" [dataSource]="element._mdbpartchilds" matSort>
<ng-container matColumnDef="{{innerColumn}}" *ngFor="let innerColumn of innerDisplayedColumns">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{innerColumn}} </th>
<td mat-cell *matCellDef="let element"> {{element[innerColumn]}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="innerDisplayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: innerDisplayedColumns;"></tr>
</table>
</div>
</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let element; columns: displayedColumns;" [class.example-element-row]="element._mdbpartchilds?.length"
[class.example-expanded-row]="expandedElement === element" (click)="toggleRow(element)">
</tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
But assuming if child element also contains some sub child rows how one should implement the logic ? The data for each level can be fetched on row or mat-cell click and data source will get bind to but I see a challenge here to update whole data source when new data arrives for nested table.
To summarize the items can have unpredictable no. of child items which should be displayed if child count is greater than 0. Any help would greatly appreciated.**