I've created an Angular Material Data Table with this ng generate @angular/material:material-table
command and it gave me following file structure:
- table-datasource.ts
- table.component.ts
- table.component.html
The idea here is to do all the fetching, sorting and pagination in the table-datasource.ts
. By default the data is placed in an Array inside table-datasource.ts
but in my case its coming from an ngxs-store which exposes an Observable of an Array. Atm I have following implementation:
table-datasource.ts:
export class TokenTableDataSource extends DataSource<TokenTableItem> {
@Select(TokenTableState.getTokenTableItems) private tokenTableItems$:Observable<TokenTableItem[]>;
totalItems$ = new BehaviorSubject<TokenTableItem[]>([]);
constructor(private paginator: MatPaginator, private sort: MatSort) {
super();
}
/**
* Connect this data source to the table. The table will only update when
* the returned stream emits new items.
* @returns A stream of the items to be rendered.
*/
connect(): Observable<TokenTableItem[]> {
this.tokenTableItems$.subscribe(item => this.totalItems$.next(item));
// init on first connect
if (this.totalItems$.value === undefined) {
this.totalItems$.next([]);
this.paginator.length = this.totalItems$.value.length;
}
// Combine everything that affects the rendered data into one update
// stream for the data-table to consume.
const dataMutations = [
observableOf(this.totalItems$),
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(
map(() => this.totalItems$.next(this.getPagedData(this.getSortedData([...this.totalItems$.value])))),
mergeMap(() => this.totalItems$)
);
}
...generated paging and sorting methods
table-component.html:
<div class="mat-elevation-z8">
<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">
...multiple columns
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator #paginator
[length]="this.dataSource.totalItems$.value?.length"
[pageIndex]="pageIndex"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
[showFirstLastButtons]=true
(page)="handlePage($event)">
</mat-paginator>
</div>
table.component.ts:
export class TokenTableComponent implements OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
dataSource: TokenTableDataSource;
pageSizeOptions = [5, 10, 20, 40];
pageSize = this.pageSizeOptions[0];
pageIndex = 0;
tableLength = 0;
... colums definition
ngOnInit(): void {
this.dataSource = new TokenTableDataSource(this.paginator, this.sort);
}
public handlePage(pageEvent: PageEvent) {
// do what?
}
}
What's working:
- The data is rendered correct (triggered with a button and via the ngxs-store)
- I can sort the data
What's not working:
- On first data load the pageSize is ignored at all and all rows are displyed
- When clicking sorting or a pagination element, the current selected
pageSize
is taken and this amount of rows is rendered. What's strange to me is that this only works descending (givenpageSize
is 10 and I select 5 it results in 5 rows but once 5 is selected it's not possible to display more rows than 5 again)
Requirements:
- I like the idea to encapsulate all data manipulations behind
TableDataSource.connect()
so a solution like this where the fetching is done in the comonent is not desired. Furthermore this doesn't have sorting implemented. - The app uses an ngxs-store, which is very similar to ngrx, so any solution involving this part is welcome.
- I haven't figured out what to do with pageEvents so my guess is that the solution is in the
handlePage()
method.
Versions:
- RxJS 6.3.x
- Angular 7.x
- ngxs 3.3.x