-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.js
62 lines (49 loc) · 1.47 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
const TABLE_HEADERS = 'table_headers';
const TABLE_BODY = 'table_body';
function capitalizeHeader(header) {
return header[0].toUpperCase() + header.slice(1).toLowerCase();
}
function createTableHeaderNames(data) {
return Object.keys(data[0])
.map((h) => h.replaceAll('_', ' '))
.map(capitalizeHeader);
}
function createHeaderElement(header) {
const th = document.createElement('th');
th.innerText = header;
th.setAttribute('scope', 'col');
return th;
}
function createTableDataElement(tableData) {
const td = document.createElement('td');
td.innerHTML = tableData;
return td;
}
function createRow(rowData) {
const tr = document.createElement('tr');
Object.values(rowData)
.map(createTableDataElement)
.forEach((el) => tr.appendChild(el));
return tr;
}
export function tableBuilder(rows) {
const tableHeadersElement = document.getElementById(TABLE_HEADERS);
createTableHeaderNames(rows)
.map(createHeaderElement)
.forEach((el) => tableHeadersElement.append(el));
appendToTable(rows);
}
export const appendToTable = (rows) => {
const tableBody = document.getElementById(TABLE_BODY);
rows.map(createRow).forEach((row) => tableBody.append(row));
};
export const getTableData = async (api_url) => {
return await (await fetch(api_url)).json();
};
const clearElement = (elementId) => {
document.getElementById(elementId).innerHTML = '';
};
export const clearTable = () => {
clearElement(TABLE_HEADERS);
clearElement(TABLE_BODY);
};