Faaaah (copy to edit)
This commit is contained in:
41
lab1/JavaScript/table.js
Normal file
41
lab1/JavaScript/table.js
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user