Files
uni-web-site/lab1/JavaScript/table.js
2026-02-26 20:12:56 +10:00

42 lines
1.1 KiB
JavaScript

const createTable = (data, idTable) => {
const table = document.getElementById(idTable);
const header = Object.keys(data[0]);
/* создание шапки таблицы */
const headerRow = createHeaderRow(header);
table.append(headerRow);
/* создание тела таблицы */
const bodyRows = createBodyRows(data);
table.append(bodyRows);
};
const clearTable = (idTable) => {
const table = document.getElementById(idTable);
table.removeChild(table.children[1]);
}
const createHeaderRow = (headers) => {
const tr = document.createElement('tr');
headers.forEach(header => {
const th = document.createElement('th');
th.innerHTML = header;
tr.append(th);
});
return tr;
};
const createBodyRows = (rows) =>{
const body = document.createElement('tbody');
rows.forEach(row =>{
const rowElement = document.createElement('tr');
for (let key in row) {
const td = document.createElement('td');
td.innerHTML = row[key];
rowElement.appendChild(td);
}
body.appendChild(rowElement);
})
return body;
}