Relationships
Depends on
-
MatTableModule
-
MatButtonModule
-
FontAwesomeModule
|
dataSource
|
Type : unknown
|
Default value : new MatTreeFlatDataSource(this.treeControl, this.treeFlattener)
|
|
|
|
displayedColumns
|
Type : string[]
|
Default value : ["name", "count"]
|
|
|
|
getGroupedByString
|
Type : unknown
|
Default value : getGroupingInformationString
|
|
|
|
treeControl
|
Type : unknown
|
Default value : new FlatTreeControl<FlattenedReportRow>(
(row) => row.level,
(row) => row.isExpandable,
)
|
|
|
|
treeFlattener
|
Type : unknown
|
Default value : new MatTreeFlattener<ReportRow, FlattenedReportRow>(
(row, level) => ({
level: level,
isExpandable: !!row.subRows && row.subRows.length > 0,
...row,
}),
(row) => row.level,
(row) => row.isExpandable,
(row) => row.subRows,
)
|
|
|
import {
Component,
ChangeDetectionStrategy,
effect,
input,
} from "@angular/core";
import { getGroupingInformationString, ReportRow } from "../../report-row";
import { FlatTreeControl } from "@angular/cdk/tree";
import {
MatTreeFlatDataSource,
MatTreeFlattener,
} from "@angular/material/tree";
import { MatTableModule } from "@angular/material/table";
import { MatButtonModule } from "@angular/material/button";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
interface FlattenedReportRow extends ReportRow {
level: number;
isExpandable: boolean;
}
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "app-report-row",
templateUrl: "./report-row.component.html",
styleUrls: ["./report-row.component.scss"],
imports: [MatTableModule, MatButtonModule, FontAwesomeModule],
})
export class ReportRowComponent {
rows = input<ReportRow[]>([]);
displayedColumns: string[] = ["name", "count"];
getGroupedByString = getGroupingInformationString;
treeFlattener = new MatTreeFlattener<ReportRow, FlattenedReportRow>(
(row, level) => ({
level: level,
isExpandable: !!row.subRows && row.subRows.length > 0,
...row,
}),
(row) => row.level,
(row) => row.isExpandable,
(row) => row.subRows,
);
treeControl = new FlatTreeControl<FlattenedReportRow>(
(row) => row.level,
(row) => row.isExpandable,
);
dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
constructor() {
effect(() => {
this.dataSource.data = this.rows();
});
}
}
<table mat-table [dataSource]="dataSource" class="full-width">
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>
<span style="padding-left: 40px" i18n="Table report header"> Name </span>
</th>
<td
mat-cell
*matCellDef="let node"
class="flex-row align-center"
style="height: 52px"
>
<button
mat-icon-button
[style.visibility]="node.isExpandable ? '' : 'hidden'"
[style.marginLeft.px]="node.level * 32"
(click)="treeControl.toggle(node)"
[attr.aria-label]="'Toggle ' + node.header.label"
>
<fa-icon
class="expand-icon-button"
[icon]="
treeControl.isExpanded(node) ? 'chevron-down' : 'chevron-right'
"
></fa-icon>
</button>
{{ node.header.label }} {{ getGroupedByString(node.header.groupedBy) }}
</td>
</ng-container>
<ng-container matColumnDef="count">
<th mat-header-cell *matHeaderCellDef i18n="Table report header">Count</th>
<td mat-cell *matCellDef="let node">{{ node.header.result }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
@use "variables/colors";
table tr:nth-child(even) {
background-color: colors.$grey-light;
}
.expand-icon-button {
color: colors.$muted;
}
Legend
Html element with directive