-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.js
163 lines (149 loc) · 6.42 KB
/
table.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
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
// Table extension, https://github.com/GiovanniSalmeri/yellow-table
"use strict";
document.addEventListener("DOMContentLoaded", function() {
/// filtrable
function filterOn(input) {
var filter = input.value.toLowerCase();
var id = input.getAttribute("data-table");
var table = document.getElementById(id);
var rows = table.querySelectorAll("tbody tr");
rows.forEach(function(row) {
if (row.textContent.toLowerCase().indexOf(filter) > -1) {
row.removeAttribute("aria-hidden");
} else {
row.setAttribute("aria-hidden", true);
}
});
}
var tables = document.querySelectorAll("table.filtrable");
tables.forEach(function(table, i) {
table.id = table.id || "table-filtrable-"+i;
var filterInput = document.querySelector("input.filter[data-table=\""+table.id+"\"]");
if (!filterInput) {
filterInput = document.createElement("input");
filterInput.type = "text";
filterInput.setAttribute("role", "search");
filterInput.className = "filter form-control";
filterInput.dataset["table"] = table.id;
var tableCaption = table.querySelector("caption");
if (!tableCaption) {
tableCaption = document.createElement("caption");
table.insertAdjacentElement("afterbegin", tableCaption);
}
tableCaption.insertAdjacentElement("beforeend", filterInput);
}
filterInput.addEventListener("input", function(e) {
// together with display: none emulate visibility: collapse
var id = e.target.getAttribute("data-table");
var table = document.getElementById(id);
var headers = table.querySelectorAll("thead th");
if (!headers[0].style.width) { // only if necessary
headers.forEach(function(header) {
// minWidth instead of width avoids strange quirks in Blink
header.style.minWidth = window.getComputedStyle(header).getPropertyValue("width");
});
}
filterOn(e.target);
}, false);
});
window.addEventListener("resize", function() {
var headers = document.querySelectorAll("table.filtrable thead tr th");
headers.forEach(function(header) {
header.style.removeProperty("width");
});
});
/// sortable
function tableSort(header) {
var table = header.closest("table");
// get Table Data
var rows = table.querySelectorAll("tbody tr");
var tableData = [];
rows.forEach(function(row, i) {
tableData[i] = {};
tableData[i]["index"] = i;
for (var j = 0, m = row.cells.length; j < m; j++) {
tableData[i][j] = row.cells[j].innerText.trim();
}
});
// sort Table Data
var sortOrder = header.getAttribute("aria-sort") == "ascending" ? -1 : 1;
var colNo = header.cellIndex;
var coll = new Intl.Collator([document.documentElement.lang, "en"], { numeric: true, sensitivity: "base", ignorePunctuation: true }).compare;
tableData.sort(function(a, b) { return coll(a[colNo], b[colNo])*sortOrder; });
// rewrite Table HTML
var html = "";
tableData.forEach(function(line) {
html += rows[line["index"]].outerHTML;
});
table.querySelector("tbody").innerHTML = html;
// remove TH Class
table.querySelectorAll("thead th").forEach(function(header) {
header.removeAttribute("aria-sort");
});
// set TH Class
var sortLabel = sortOrder === 1 ? "ascending" : "descending";
header.setAttribute("aria-sort", sortLabel);
}
var tables = document.querySelectorAll("table.sortable");
tables.forEach(function(table) {
var ths = table.querySelectorAll("th");
ths.forEach(function(th) {
th.setAttribute("tabindex", 0);
});
table.setAttribute("role", "grid");
var headers = table.querySelectorAll("thead tr");
headers.forEach(function(header) {
tableSort(header.cells[0]);
for (var j = 0, m = header.cells.length; j < m; j++) {
header.cells[j].addEventListener("click", function(e) { tableSort(e.currentTarget); }, false);
header.cells[j].addEventListener("keydown", function(e) { if (e.which === 13) this.click(); }, false);
}
});
});
/// point-aligned
var tables = document.querySelectorAll("table.point-aligned");
tables.forEach(function(table) {
for (var c = 1; ; c++) {
var cells = table.querySelectorAll("td:nth-child(" + c + ")");
if (!cells.length) break;
var maxDec = 0;
var thereAreDec = false;
var numOfDec = {};
cells.forEach(function(cell, i) {
var content = cell.innerText.trim().replace("\u00A0", "");
if (!isNaN(content) && content !== "") {
var dec = content.indexOf(".");
if (dec > -1) {
thereAreDec = true;
numOfDec[i] = content.length - dec - 1;
maxDec = Math.max(maxDec, numOfDec[i]);
} else {
numOfDec[i] = 0;
}
}
});
cells.forEach(function(cell, i) {
var padding = "";
if (numOfDec[i] || thereAreDec) {
for (var d = numOfDec[i]; d < maxDec; d++) {
if (d % 3 == 0) padding += "\u00A0"; // no-break space
padding += "\u2007"; // figure space
}
}
if (numOfDec[i]) {
cell.innerText += padding;
} else if (thereAreDec) {
cell.innerText += "\u2008" + padding.substr(1); // punctuation space
}
});
}
});
/// set tabindex (for accessibility)
window.addEventListener("resize", function() {
var divs = document.querySelectorAll("div.table-container");
divs.forEach(function(div, i) {
div.setAttribute("tabindex", div.scrollWidth > div.clientWidth ? 0 : null);
});
});
window.dispatchEvent(new Event("resize"));
}, false);