a6a515b570173edc7b06d2de21b01a17f963caf4
[portal/sdk.git] /
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';
6
7 // TODO: Replace this with your own data model type
8 export interface RunReportFinalTableItem {
9   
10 }
11
12 // TODO: replace this with real data from your application
13
14
15 /**
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).
19  */
20 export class RunReportFinalTableDataSource extends DataSource<RunReportFinalTableItem> {
21   data: RunReportFinalTableItem[];
22   paginator: MatPaginator;
23   sort: MatSort;
24
25   constructor() {
26     super();
27   }
28
29   /**
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.
33    */
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),
39       this.paginator.page,
40       this.sort.sortChange
41     ];
42
43     return merge(...dataMutations).pipe(map(() => {
44       return this.getPagedData(this.getSortedData([...this.data]));
45     }));
46   }
47
48   /**
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.
51    */
52   disconnect() {}
53
54   /**
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.
57    */
58   private getPagedData(data: RunReportFinalTableItem[]) {
59     const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
60     return data.splice(startIndex, this.paginator.pageSize);
61   }
62
63   /**
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.
66    */
67   private getSortedData(data: RunReportFinalTableItem[]) {
68     if (!this.sort.active || this.sort.direction === '') {
69       return data;
70     }
71
72     return data.sort((a, b) => {
73       const isAsc = this.sort.direction === 'asc';
74       switch (this.sort.active) {
75         // case 'name': return compare(a.name, b.name, isAsc);
76         // case 'id': return compare(+a.id, +b.id, isAsc);
77         default: return 0;
78       }
79     });
80   }
81 }
82
83 /** Simple sort comparator for example ID/Name columns (for client-side sorting). */
84 function compare(a, b, isAsc) {
85   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
86 }