-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreetable.js
More file actions
172 lines (147 loc) · 5.47 KB
/
treetable.js
File metadata and controls
172 lines (147 loc) · 5.47 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(function() {
'use strict';
var input = {
columns: [
'Status',
'Kunde',
'Datum',
'Abteilung',
'Personalnummer'
],
models: [{
status: 'Bearbeiten',
children: [{
typ: 'Abrechnung',
kunde: 'Witt, Frauke',
datum: '18.05 - 19.05',
abteilung: 'Vertrieb',
personalnummer: 'C2420'
}, {
typ: 'Aufnahme',
kunde: 'Meier, Hans',
datum: '20.05',
abteilung: 'Vertrieb',
personalnummer: 'C1309'
}, {
status: 'Pausiert',
children: [{
typ: 'Abrechnung',
kunde: 'Witt, Frauke',
datum: '18.05 - 19.05',
abteilung: 'Vertrieb',
personalnummer: 'C2400'
}]
}]
}, {
status: 'Kontrollieren',
children: [{
typ: 'Gespräch',
kunde: 'Peterson, Friedrich',
datum: '05.04 - 10.04',
abteilung: 'Verkauf',
personalnummer: 'IN35'
}, {
typ: 'Abrechnung',
kunde: 'Witt, Frauke',
datum: '18.05 - 19.05',
abteilung: 'Vertrieb',
personalnummer: 'C244'
}]
}, {
status: 'Abgeschlossen',
children: [{
typ: 'Gespräch',
kunde: 'Peterson, Friedrich',
datum: '05.04 - 10.04',
abteilung: 'Verkauf',
personalnummer: 'IN35'
}, {
typ: 'Abrechnung',
kunde: 'Witt, Frauke',
datum: '18.05 - 19.05',
abteilung: 'Vertrieb',
personalnummer: 'C244'
}]
}]
}
function createTreeTable(columns, models) {
var tt = document.createElement('table');
tt.classList.add('tree-table');
var thead = document.createElement('thead');
var theadRow = document.createElement('tr');
thead.appendChild(theadRow);
columns.forEach(function(columnName) {
var headCol = document.createElement('td');
headCol.innerHTML = columnName;
theadRow.appendChild(headCol);
});
tt.appendChild(thead);
var nodes = createNodes(models);
nodes.forEach(function(node) {
tt.appendChild(node);
});
return tt;
};
function createNodes(models, layer) {
layer = layer || 0;
var nodes = [];
models.forEach(function(model, index) {
var node = document.createElement('tr');
node.classList.add('layer-' + layer);
var hasChildren = model.hasOwnProperty('children');
var childNodes = null;
if (hasChildren)
childNodes = createNodes(model.children, layer + 1);
Object.keys(model).forEach(function(key, keyIndex) {
if (key !== 'children') {
var cell = document.createElement('td');
cell.classList.add('tt-cell');
if (keyIndex === 0 && hasChildren) {
cell.classList.add('tt-node');
var expandBtn = document.createElement('span');
expandBtn.classList.add('expand-btn');
expandBtn.innerHTML = '-';
function createExpandHandler(childNodes, btn) {
return function() {
if (btn.innerHTML == '-') {
childNodes.forEach(function(childNode) {
childNode.classList.add('hidden');
});
expandBtn.innerHTML = '+';
} else if (btn.innerHTML == '+') {
childNodes.forEach(function(childNode) {
childNode.classList.remove('hidden');
});
expandBtn.innerHTML = '-';
}
};
}
expandBtn.addEventListener('click', createExpandHandler(childNodes, expandBtn));
cell.appendChild(expandBtn);
}
cell.appendChild(document.createTextNode(model[key]));
node.appendChild(cell);
}
});
nodes.push(node);
if (hasChildren) {
var childContainer = document.createElement('tbody');
var subChilds = null;
childNodes.forEach(function(childNode) {
if (childNode.tagName !== 'TBODY')
childContainer.appendChild(childNode);
else
subChilds = childNode;
});
nodes.push(childContainer);
if (subChilds)
nodes.push(subChilds);
}
});
return nodes;
};
window.onload = function() {
var tt = createTreeTable(input.columns, input.models);
document.body.appendChild(tt);
};
})();