-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspose-table.js
More file actions
80 lines (64 loc) · 2.23 KB
/
transpose-table.js
File metadata and controls
80 lines (64 loc) · 2.23 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
;(function (window, document, $) {
var switched = false;
var tableSwapTriggerWidth = 1030;
updateTables();
$(window).resize(function () {
updateTables();
});
function updateTables() {
if (($(window).width() < tableSwapTriggerWidth) && !switched) {
switched = true;
$("table.ResponsiveTable").each(function (i, table) {
reorganizeTable($(table));
});
return true;
}
else if (switched && ($(window).width() > tableSwapTriggerWidth)) {
switched = false;
$("table.ResponsiveTable").each(function (i, table) {
removeReorganizedTable($(table));
});
}
}
function reorganizeTable(original) {
// TODO: update to handle more than a single data row
var $table = $('<table class="small"/>'),
rowTemplate = '<tr><td class="heading">{{0}}</td><td>{{1}}</td></tr>',
header = [],
row = [];
// get first row (header); grab each cell; append to new table
original.find("tr").eq(0).find("td,th").each(function () {
header.push($(this).clone().html());
});
// get data row; get each cell; append to new table
original.find("tr").eq(1).find("td").each(function () {
row.push($(this).clone().html());
});
$.each(header, function (id) {
$table.append(doTemplate(rowTemplate, [header[id], row[id]]));
});
original.after($table);
}
function removeReorganizedTable(original) {
original
.parent()
.find(".small")
.remove();
}
/*
simple HTML templating function
array example:
demo.markup("<div>{{1}}, {{0}}</div>", ["John", "Doe"]);
object example:
demo.markup("<div>{{last}}, {{first}}</div>", {first:"John", last:"Doe"});
*/
function doTemplate(html, data) {
var m;
var i = 0;
var match = html.match(data instanceof Array ? /{{\d+}}/g : /{{\w+}}/g) || [];
while (m = match[i++]) {
html = html.replace(m, data[m.substr(2, m.length - 4)]);
}
return html;
}
}(this, document, jQuery));