-
Notifications
You must be signed in to change notification settings - Fork 581
/
dataset-simple-table.component.ts
149 lines (122 loc) · 3.72 KB
/
dataset-simple-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
138
139
140
141
142
143
144
145
146
147
148
149
import {Component, Input} from "@angular/core";
import {ITdDataTableSortChangeEvent, TdDataTableService, TdDataTableSortingOrder} from "@covalent/core/data-table";
import {SimpleChanges} from "@angular/core/src/metadata/lifecycle_hooks";
import {TableColumn} from "./model/table-view-model";
@Component({
selector: 'dataset-simple-table',
styleUrls:["./dataset-simple-table.component.scss"],
template:`
<td-data-table
[data]="filteredData"
[columns]="columns"
[selectable]="false"
[sortable]="true"
[sortBy]="sortBy"
[sortOrder]="sortOrder"
(sortChange)="sort($event)"
[style.height.px]="325" class="dataset-simple-table">
</td-data-table>
<div *ngIf="filteredData.length == 0" fxLayout="row" fxLayoutAlign="center center">
<h3>No results to display.</h3>
</div>`
})
export class DatasetSimpleTableComponent {
@Input()
rows:any[];
@Input()
columns:TableColumn[] = [];
constructor( private _dataTableService: TdDataTableService){
}
/**
* All the data
* @type {any[]}
*/
data:any[] = [];
/**
* sorted/filtered data displayed in the ui
* @type {any[]}
*/
filteredData:any[] = [];
/**
* Calc the column width based upon data?
*/
@Input()
calcColumnWidth:boolean = true;
sortBy: string = '';
sortOrder: TdDataTableSortingOrder = TdDataTableSortingOrder.Descending;
sort(sortEvent: ITdDataTableSortChangeEvent): void {
this.sortBy = sortEvent.name;
this.sortOrder = sortEvent.order;
this.filter();
}
filter(){
let newData:any[] = this.data;
newData = this._dataTableService.sortData(newData, this.sortBy, this.sortOrder);
this.filteredData = newData;
}
ngOnInit(){
this.initTable();
}
ngOnChanges(changes: SimpleChanges) {
if(changes && (!changes.rows.firstChange || !changes.columns.firstChange)){
this.initTable();
}
}
initTable(){
if(this.columns) {
this.sortBy = this.columns[0].name;
}
else {
this.columns = [];
}
//determine width from data
if(this.calcColumnWidth && this.columns.length >3) {
let colWidths = this.calculateMaxColumnWidth();
this.columns.forEach(col => {
let width = colWidths[col.name]
let pixels = 0;
if (width == 0) {
pixels = 100;
}
else {
pixels = width * 11;
}
if (pixels > 300) {
pixels = 300;
}
if (pixels < 150) {
pixels = 150;
}
col.width = pixels;
});
}
// Add table data
this.data = this.rows;
this.filter();
}
/**
* Calc the max width based upon data
* @return {any}
*/
calculateMaxColumnWidth():any{
let widthMap = {};
this.rows.map(row => {
this.columns.forEach(col => {
let columnValue = row[col.name];
let max = widthMap[col.name];
let currWidth = 0
if (max == undefined) {
max = 0;
widthMap[col.name] = 0;
}
if (columnValue != undefined && columnValue != null) {
currWidth = (columnValue+"").length;
}
if (currWidth > max) {
widthMap[col.name] = currWidth;
}
});
});
return widthMap;
}
}