-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv-table.mjs
56 lines (51 loc) · 1.17 KB
/
csv-table.mjs
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
import { CSV } from "https://js.sabae.cc/CSV.js";
class CSVTable extends HTMLElement {
async getCSV() {
const fn = this.getAttribute("src");
if (fn) {
const data = await CSV.fetch(fn);
return data;
}
const txt = this.textContent.trim();
const data = CSV.decode(txt);
this.textContent = "";
return data;
}
constructor () {
super();
this.init();
}
async init() {
const ss = await this.getCSV();
const c = tag => document.createElement(tag);
const tbl = c("table");
let firstline = true;
for (const s of ss) {
const tr = c("tr");
tbl.append(tr);
for (const s3 of s) {
const td = c(firstline ? "th" : "td");
tr.appendChild(td);
td.textContent = s3;
}
tbl.appendChild(tr);
firstline = false;
}
this.appendChild(tbl);
// css
const style = c('style');
style.textContent = `
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #333;
padding: .1em .5em;
font-size: 90%;
}
`;
this.appendChild(style);
}
}
customElements.define('csv-table', CSVTable);
export { CSVTable };