-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtablecreator.js
119 lines (109 loc) · 3.51 KB
/
tablecreator.js
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
var COLUMN_FORMAT_STRING = "COLUMN_FORMAT_STRING";
var COLUMN_FORMAT_NUMBER = "COLUMN_FORMAT_NUMBER";
var COLUMN_FORMAT_PERCENT = "COLUMN_FORMAT_PERCENT";
var COLUMN_FORMAT_PERCENT_FLOAT = "COLUMN_FORMAT_PERCENT_FLOAT";
class TableHeader {
constructor() {
this.columnName = null;
this.propertyName = null;
this.columnFormat = COLUMN_FORMAT_STRING;
this.comparePropertyName = null;
this.sortAscending = false;
}
}
class TableCreator {
constructor(tableCreatorID, tableParentElement) {
this.tableCreatorID = tableCreatorID;
this.tableParentElement = tableParentElement;
this.defaultValue = "-";
this.reset();
if (TableCreator.all == null) {
TableCreator.all = [];
}
TableCreator.all.push(this);
}
reset() {
this.headerHTML = "";
this.dataHTML = "";
this.tableHeaders = [];
this.cssClass = "";
this.topUnsortableRows = [];
this.data = [];
}
static sortAndRecreateTable(tableCreatorID, comparePropertyName) {
var tableCreator = null;
for (var tc of TableCreator.all) {
if (tc.tableCreatorID = tableCreatorID) {
tableCreator = tc;
break;
}
}
if (tableCreator == null) {
console.log("ERROR: cannot sort for table creator w/ id '" + tableCreatorID + "', table creator doesn't exist.", TableCreator.all);
return;
}
tableCreator.sortData(comparePropertyName);
debug("Finished sorting table creator w/ id '" + tableCreatorID + " by property '" + comparePropertyName + "'. Re-creating table.");
tableCreator.createTable();
}
sortData(comparePropertyName) {
debug("Sorting table creator w/ id '" + this.tableCreatorID + " by property '" + comparePropertyName + "'.");
for (var header of this.tableHeaders) {
if (header.comparePropertyName == comparePropertyName) {
sortItems(this.data, comparePropertyName, header.sortAscending);
}
}
}
createTable() {
var html = "";
html += "<table id='theTable' class='" + this.cssClass + "'><thead>\n";
html += this.headerHTML;
html += "</thead><tbody>\n";
html += this.renderData();
html += "</tbody></table>\n"
this.tableParentElement.innerHTML = html;
}
addTableHeader(columnName, propertyName, columnFormat, comparePropertyName) {
columnFormat = columnFormat == null ? COLUMN_FORMAT_STRING : columnFormat;
var header = new TableHeader();
header.columnName = columnName;
header.propertyName = propertyName;
if (columnFormat != null) {
header.columnFormat = columnFormat;
}
header.comparePropertyName = comparePropertyName;
this.tableHeaders.push(header);
this.headerHTML += "<th";
if (comparePropertyName != null) {
this.headerHTML += " onclick=\"TableCreator.sortAndRecreateTable('"
+ this.tableCreatorID + "', '" + comparePropertyName + "')\"";
}
this.headerHTML += ">" + columnName + "</th>\n";
return header;
}
renderData() {
if (this.data == null && this.topUnsortableRows == null) {
return "";
}
var html = "";
var rows = this.topUnsortableRows.concat(this.data);
for (var record of rows) {
html += "<tr>";
for (var header of this.tableHeaders) {
var value = record[header.propertyName];
if (value == null) {
value = this.defaultValue;
} else if (COLUMN_FORMAT_NUMBER == header.columnFormat) {
value = value.toLocaleString();
} else if (COLUMN_FORMAT_PERCENT == header.columnFormat) {
value = "" + value + "%";
} else if (COLUMN_FORMAT_PERCENT_FLOAT == header.columnFormat) {
value = "" + (value * 100.0).toFixed(2) + "%";
}
html += "<td>" + value + "</td>";
}
html += "</tr>\n";
}
return html
}
}