-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayToTable.js
More file actions
70 lines (51 loc) · 2.04 KB
/
ArrayToTable.js
File metadata and controls
70 lines (51 loc) · 2.04 KB
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
function ArrayToTable() { // ArrayToTable class definiton
var self = this;
this.replaceRules = {};
this.tableclasses = '';
this.tableid = '';
} // ArrayToTable class definition END
ArrayToTable.prototype.putTableHtmlToId = function(dataArray, id) {
var tableDiv = document.getElementById(id);
tableDiv.innerHTML = '';
tableDiv.innerHTML = this.getHtmlForArray(dataArray);
};
ArrayToTable.prototype.getHtmlForArray = function(dataArray) {
var self = this;
if (Object.keys(dataArray).length === 0) {
return '';
}
var newhtml = '<table';
if (this.tableclasses !== '') {
newhtml += ' class="' + this.tableclasses + '"';
}
if (this.tableid !== '') {
newhtml += ' id="' + this.tableid + '"';
}
newhtml += '>';
newhtml += '<thead><tr>';
var footer = '<tfoot><tr>';
Object.keys(dataArray[0]).forEach(function(col) {
if (typeof(self.replaceRules) === 'undefined' || typeof(self.replaceRules[col]) === 'undefined') {
newhtml += '<th class="col_' + col.trim().replace(/\s+/g, '') + '">' + col + '</th>';
footer += '<th class="col_' + col.trim().replace(/\s+/g, '') + '">' + col + '</th>';
} else {
newhtml += '<th class="col_' + col.trim().replace(/\s+/g, '') + '">' + self.replaceRules[col] + '</th>';
footer += '<th class="col_' + col.trim().replace(/\s+/g, '') + '">' + self.replaceRules[col] + '</th>';
}
});
newhtml += '</tr></thead>';
footer += '</tr></tfoot>';
Object.keys(dataArray).forEach(function(row) {
newhtml += '<tr>';
Object.keys(dataArray[row]).forEach(function(col) {
newhtml += '<td class="col_' + col.trim().replace(/\s+/g, '') + '">';
newhtml += dataArray[row][col];
newhtml += '</td>';
});
newhtml += '</tr>';
});
newhtml += footer;
newhtml += '</table>';
return newhtml;
};
// end define once prototypes of class ArrayToTable