1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
import { DataSource } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { map } from 'rxjs/operators';
import { Observable, of as observableOf, merge } from 'rxjs';
// TODO: Replace this with your own data model type
export interface AllReportsItem {
rep_name: string;
descr : string;
owner : string;
create_date : string;
copy : string;
edit : string;
delete : string;
schedule : string;
run : string;
rep_id : number;
}
// TODO: replace this with real data from your application
var EXAMPLE_DATA: AllReportsItem[];
/**
* Data source for the AllReports view. This class should
* encapsulate all logic for fetching and manipulating the displayed data
* (including sorting, pagination, and filtering).
*/
export class AllReportsDataSource extends DataSource<AllReportsItem> {
data: AllReportsItem[];
paginator: MatPaginator;
sort: MatSort;
toggle : boolean;
constructor() {
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<AllReportsItem[]> {
// Combine everything that affects the rendered data into one update
// stream for the data-table to consume.
const dataMutations = [
observableOf(this.data),
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(map(() => {
return this.getPagedData(this.getSortedData([...this.data]));
}));
}
/**
* Called when the table is being destroyed. Use this function, to clean up
* any open connections or free any held resources that were set up during connect.
*/
disconnect() {}
/**
* Paginate the data (client-side). If you're using server-side pagination,
* this would be replaced by requesting the appropriate data from the server.
*/
private getPagedData(data: AllReportsItem[]) {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
return data.splice(startIndex, this.paginator.pageSize);
}
/**
* Sort the data (client-side). If you're using server-side sorting,
* this would be replaced by requesting the appropriate data from the server.
*/
private getSortedData(data: AllReportsItem[]) {
if (!this.sort.active || this.sort.direction === '') {
return data;
}
return data.sort((a, b) => {
const isAsc = this.sort.direction === 'asc';
switch (this.sort.active) {
case 'rep_id': return compare(+a.rep_id, +b.rep_id, isAsc);
case 'rep_name': return compare(a.rep_name, b.rep_name, isAsc);
case 'descr': return compare(a.descr, b.descr, isAsc);
case 'owner': return compare(a.owner, b.owner, isAsc);
case 'create_date': return compare(a.create_date, b.create_date, isAsc);
case 'copy': return compare(a.copy, b.copy, isAsc);
case 'edit': return compare(a.edit, b.edit, isAsc);
case 'delete': return compare(a.delete, b.delete, isAsc);
case 'schedule': return compare(a.schedule, b.schedule, isAsc);
case 'run': return compare(a.run, b.run, isAsc);
default: return 0;
}
});
}
}
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
function compare(a, b, isAsc) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
|