I have the below data
[
{
"_id": "c9d5ab1a",
"subdomain": "wing",
"domain": "aircraft",
"part_id": "c9d5ab1a",
"info.mimetype": "application/json",
"info.dependent": "parent",
"nested": [
{
"domain": "aircraft",
"_id": "c1859902",
"info.mimetype": "image/jpeg",
"info.dependent": "c9d5ab1a",
"part_id": "c1859902",
"subdomain": "tail"
}
]
},
{
"_id": "1b0b0a26",
"subdomain": "fuel",
"domain": "aircraft",
"part_id": "1b0b0a26",
"info.mimetype": "image/jpeg",
"info.dependent": "no_parent"
}
]
Here if "info.dependent": "parent"
then it is nested and if "info.dependent": "no_parent"
then it does not have a child. I tried to create a dynamic table but I am stuck on how to make it collapsible/expandable with a nested table.
Here is my code on stackblitz.
<mat-table class=" mat-elevation-z8" [dataSource]="dataSource">
<ng-container [matColumnDef]="col" *ngFor="let col of displayedColumns">
<mat-header-cell *matHeaderCellDef> {{ col }} </mat-header-cell>
<mat-cell *matCellDef="let element"> {{ element[col] }} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row;columns:displayedColumns"></mat-row>
</mat-table>
.ts
public data = [
{
"_id": "c9d5ab1a",
"subdomain": "wing",
"domain": "aircraft",
"part_id": "c9d5ab1a",
"info.mimetype": "application/json",
"info.dependent": "parent",
"nested": [
{
"domain": "aircraft",
"_id": "c1859902",
"info.mimetype": "image/jpeg",
"info.dependent": "c9d5ab1a",
"part_id": "c1859902",
"subdomain": "tail"
}
]
},
{
"_id": "1b0b0a26",
"subdomain": "fuel",
"domain": "aircraft",
"part_id": "1b0b0a26",
"info.mimetype": "image/jpeg",
"info.dependent": "no_parent"
}
];
dataSource = new MatTableDataSource([]);
displayedColumns = ['_id', 'subdomain', 'domain', 'part_id', 'info.mimetype', 'info.dependent'];
constructor(){
this.displayedColumns = this.displayedColumns;
this.dataSource = new MatTableDataSource(this.data);
}
The nested format is like below
row 1 --> _id ,subdomain,domain,info.dependent
When we click on that particular row, then it has to expand and display the nested data in a table with the column names and row data.
"nested": [
{
"domain": "aircraft",
"_id": "c1859902",
"info.mimetype": "image/jpeg",
"info.dependent": "c9d5ab1a",
"part_id": "c1859902",
"subdomain": "tail"
}
]
What you actually want is to create a nested
mat-table
where all the nested tables are sortable and can be filtered through as well.Firstly, since you need to use filtering and sorting in your nested table, you need to create a new
MatTableDataSource
for it. This can be done initially when you create the maindataSource
in thengOnInit
like below.From the expandable rows example in the docs, we can see how to create an expandable row. In the expandable row, we will now have a table along with the
Filter
input. We will add some conditions so that the row is expandable only if there areaddresses
present.Now that the row expands only if there are nested elements, we need to get rid of the hover for the users which have no
addresses
Here is the CSS responsible for adding a
background-color
on hoverSo we just need to add the
example-element-row
class to our row if the row has anaddress
. If it has no address, the row should not be clickable and there should not be a hover which indicates to the user that the row is in fact not clickable.In
toggleRow
, we will define the logic for what happens when you click a row in the template. We will also implementsort
when the user clicks on the row in this function.Finally, we need to define the
applyFilter
function so the nested tables can be filtered.Here is a working example on StackBlitz.
Looking at the examples from the docs especially the one with the expandable row:
multiTemplateDataRows
directive in the<mat-table>
@detailExpand
trigger is missingHere is the example from the docs with your data
Edit (regarding the comment)
Here is how you can get the dynamic columns:
Add this to your component
use the method in the template (template updated according to the attached details screen and the note about the multiple elements under the
nested
key):