1 import { DataSource } from '@angular/cdk/collections';
2 import { MatPaginator } from '@angular/material/paginator';
3 import { MatSort } from '@angular/material/sort';
4 import { map } from 'rxjs/operators';
5 import { Observable, of as observableOf, merge } from 'rxjs';
7 // TODO: Replace this with your own data model type
8 export interface RunReportFinalTableItem {
12 // TODO: replace this with real data from your application
16 * Data source for the RunReportFinalTable view. This class should
17 * encapsulate all logic for fetching and manipulating the displayed data
18 * (including sorting, pagination, and filtering).
20 export class RunReportDataSource extends DataSource<RunReportFinalTableItem> {
21 data: RunReportFinalTableItem[];
22 paginator: MatPaginator;
30 * Connect this data source to the table. The table will only update when
31 * the returned stream emits new items.
32 * @returns A stream of the items to be rendered.
34 connect(): Observable<RunReportFinalTableItem[]> {
35 // Combine everything that affects the rendered data into one update
36 // stream for the data-table to consume.
37 const dataMutations = [
38 observableOf(this.data),
43 return merge(...dataMutations).pipe(map(() => {
44 return this.getPagedData(this.getSortedData([...this.data]));
49 * Called when the table is being destroyed. Use this function, to clean up
50 * any open connections or free any held resources that were set up during connect.
55 * Paginate the data (client-side). If you're using server-side pagination,
56 * this would be replaced by requesting the appropriate data from the server.
58 private getPagedData(data: RunReportFinalTableItem[]) {
59 const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
60 return data.splice(startIndex, this.paginator.pageSize);
64 * Sort the data (client-side). If you're using server-side sorting,
65 * this would be replaced by requesting the appropriate data from the server.
67 private getSortedData(data: RunReportFinalTableItem[]) {
68 if (!this.sort.active || this.sort.direction === '') {
73 return data.sort((a, b) => {
74 const isAsc = this.sort.direction === 'asc';
75 switch (this.sort.active) {
76 // case 'name': return compare(a.name, b.name, isAsc);
77 // case 'id': return compare(+a.id, +b.id, isAsc);
86 /** Simple sort comparator for example ID/Name columns (for client-side sorting). */
87 function compare(a, b, isAsc) {
88 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);