Relationships
Depends on
-
MatTableModule
-
MatSortModule
-
MatPaginatorModule
|
objects
|
Type : any[]
|
Default value : []
|
|
|
|
columns
|
Type : unknown
|
Default value : signal<string[]>([])
|
|
|
|
dataSource
|
Type : unknown
|
Default value : new MatTableDataSource()
|
|
|
|
paginator
|
Type : MatPaginator
|
Decorators :
@ViewChild(MatPaginator, {static: true})
|
|
|
|
sort
|
Type : MatSort
|
Decorators :
@ViewChild(MatSort, {static: true})
|
|
|
import {
Component,
ViewChild,
ChangeDetectionStrategy,
input,
signal,
effect,
} from "@angular/core";
import { MatTableDataSource, MatTableModule } from "@angular/material/table";
import { MatSort, MatSortModule } from "@angular/material/sort";
import { MatPaginator, MatPaginatorModule } from "@angular/material/paginator";
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "app-object-table",
templateUrl: "./object-table.component.html",
styleUrls: ["./object-table.component.scss"],
imports: [MatTableModule, MatSortModule, MatPaginatorModule],
})
export class ObjectTableComponent {
objects = input<any[]>([]);
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;
dataSource = new MatTableDataSource();
columns = signal<string[]>([]);
constructor() {
effect(() => {
const objs = this.objects();
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
if (objs?.length > 0) {
this.columns.set(Object.keys(objs[0]));
this.dataSource.data = objs;
} else {
this.columns.set([]);
}
});
}
}
<table
mat-table
[dataSource]="dataSource"
matSort
class="full-width"
i18n-aria-label="Label for table showing report result"
aria-label="Table showing the report results"
>
@for (col of columns(); track col) {
<ng-container [matColumnDef]="col">
<th mat-header-cell *matHeaderCellDef mat-sort-header>{{ col }}</th>
<td mat-cell *matCellDef="let row">{{ row[col] }}</td>
</ng-container>
}
<tr mat-header-row *matHeaderRowDef="columns()"></tr>
<tr mat-row *matRowDef="let row; columns: columns()"></tr>
</table>
<mat-paginator
[pageSizeOptions]="[10, 50, 100]"
showFirstLastButtons
i18n-aria-label="Label for the pagination controls of a report result table"
aria-label="Report results pagination"
>
</mat-paginator>
Legend
Html element with directive