-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-table.component.ts
137 lines (114 loc) · 4.09 KB
/
auto-table.component.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Component, ViewChild, ChangeDetectorRef, Input } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSpinner } from '@angular/material/progress-spinner';
import { MatTableDataSource } from '@angular/material/table';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { SelectionModel } from '@angular/cdk/collections';
@Component({
selector: 'auto-table',
templateUrl: './auto-table.component.html',
styleUrls: ['./auto-table.component.css'],
providers: [HttpClient]
})
export class AutoTableComponent {
apiUrl: string;
@Input() selectMode = false;
@Input() hiddenColumns = [];
selection = new SelectionModel<any>(true, []);
public dataColumns: string[] = [];
public displayColumns: string[] = [];
public inProgress = false;
public isVisible = false;
private httpHeaders: HttpHeaders;
public error: string;
public matTableDatasource = new MatTableDataSource<object>([]);
@ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
constructor(private http: HttpClient, private cdRef: ChangeDetectorRef) { }
rowClickCb = (_) => { };
loadFromServer(uri: string, query: any = null, method: string = 'GET', columns = []) {
if (this.inProgress !== true) {
this.inProgress = true;
this.cdRef.detectChanges();
}
this.matTableDatasource.data = [];
let p = new HttpParams();
for (const key in query) {
if (query.hasOwnProperty(key)) {
if (query[key]) { p = p.append(key, query[key]); }
}
}
if (method === 'GET') {
this.httpHeaders = new HttpHeaders();
this.httpHeaders.append('Content-Type', 'application/json');
this.http.get(this.apiUrl + uri, { headers: this.httpHeaders, params: p }).subscribe(
r => this.setDatasource(r, columns),
e => this.showError(e.message),
() => this.inProgress = false
);
} else if (method === 'POST') {
this.http.post(this.apiUrl + uri, p, {
headers: new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded')
}).subscribe(
r => this.setDatasource(r['payload'], columns),
e => this.showError(e.message),
() => this.inProgress = false
);
}
}
rowClicked(row: any) {
this.rowClickCb(row);
}
private showError(message: string) {
this.error = 'An error occured while loading the data.';
console.error(message);
this.inProgress = false;
this.isVisible = false;
}
private setDatasource(data: any, columns = []) {
this.error = '';
this.dataColumns = [];
this.displayColumns = [];
columns.forEach(item => { this.dataColumns.push(item); });
if (columns.length === 0 && data && data[0]) {
Object.keys(data[0]).forEach(k => {
this.dataColumns.push(k);
});
}
if (this.selectMode) { this.displayColumns.push('select'); }
this.dataColumns.forEach(item => {
if (!this.hiddenColumns.includes(item)) {
this.displayColumns.push(item);
}
});
this.matTableDatasource.data = data;
this.matTableDatasource.paginator = this.paginator;
if (this.matTableDatasource.data && this.matTableDatasource.data.length > 0) {
this.isVisible = true;
} else {
this.isVisible = false;
}
}
public applyFilter(filterValue: string) {
this.matTableDatasource.filter = filterValue.trim().toLocaleLowerCase();
}
/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.matTableDatasource.data.forEach(row => this.selection.select(row));
}
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.matTableDatasource.data.length;
return numSelected === numRows;
}
/** The label for the checkbox on the passed row */
checkboxLabel(row?: any): string {
if (!row) {
return `${this.isAllSelected() ? 'select' : 'deselect'} all`;
}
return `${this.selection.isSelected(row) ? 'deselect' : 'select'} row ${row.position + 1}`;
}
}