-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTable.js
78 lines (75 loc) · 1.7 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
// Table is 2 dimentions array, first line is header
const Table = {};
Table.toJSON = (tbl) => {
const res = [];
const head = tbl[0];
for (let i = 1; i < tbl.length; i++) {
const d = {};
for (let j = 0; j < head.length; j++) {
d[head[j]] = tbl[i][j];
}
res.push(d);
}
return res;
};
Table.fromJSON = (json) => {
if (!Array.isArray(json)) {
throw "is not array!";
}
const head = [];
for (const d of json) {
for (const name in d) {
if (head.indexOf(name) == -1) {
head.push(name);
}
}
}
const res = [head];
for (const d of json) {
const line = [];
for (let i = 0; i < head.length; i++) {
const v = d[head[i]];
if (v == undefined) {
line.push("");
} else {
line.push(v);
}
}
res.push(line);
}
return res;
};
Table.makeTable = (ar) => {
const c = (tag) => document.createElement(tag);
const tbl = c("table");
const tr0 = c("tr");
tbl.appendChild(tr0);
for (let i = 0; i < ar[0].length; i++) {
const th = c("th");
tr0.appendChild(th);
th.textContent = ar[0][i];
}
for (let i = 1; i < ar.length; i++) {
const tr = c("tr");
tbl.appendChild(tr);
for (let j = 0; j < ar[i].length; j++) {
const td = c("td");
tr.appendChild(td);
const s = ar[i][j];
if (s.startsWith("http://") || s.startsWith("https://")) {
const a = c("a");
a.href = a.textContent = s;
td.appendChild(a);
} else if (s.startsWith("tel:")) {
const a = c("a");
a.href = s;
a.textContent = s.substring(4);
td.appendChild(a);
} else {
td.textContent = s;
}
}
}
return tbl;
};
export { Table };