function loadTable() { fetch('/printer', {method: 'GET'}).then(response => { response.json().then(json => { const tableBody = document.getElementById('table-body'); tableBody.innerHTML = null; for (printer of json) { addPrinterToTable(tableBody, printer); } }); }); } function addPrinterToTable(tableBody, printer) { const tableRow = document.createElement('tr'); addTableData(tableRow, printer.name, 'name', printer); addTableData(tableRow, `${printer.price.toFixed(2)}$`, 'price', printer); addTableData(tableRow, printer.build_volume, 'build_volume', printer); addTableData(tableRow, printer.max_speed + 'mm/s', 'max_speed', printer); addTableActions(tableRow, printer); tableRow.id = `printer${printer.id}`; tableBody.appendChild(tableRow); } function addTableActions(tableRow, printer) { const tableData = document.createElement('td'); addViewButton(tableData, printer); addEditButton(tableData, printer); addDeleteButton(tableData, printer); tableRow.appendChild(tableData); } function addEditButton(tableData, printer) { const editButton = setButtonAttributes('btn-primary', '#editModal'); editButton.id = `printer${printer.id}:editButton` editButton.textContent = 'Edit'; setEditButtonOnClick(editButton, printer); tableData.appendChild(editButton); } function addViewButton(tableData, printer) { const viewButton = setButtonAttributes('btn-primary', '#viewModal'); viewButton.textContent = 'View'; viewButton.id = `printer${printer.id}:viewButton`; setViewButtonOnClick(viewButton, printer); tableData.appendChild(viewButton); } function addDeleteButton(tableData, printer) { const deleteButton = setButtonAttributes('btn-danger', '#deleteModal'); deleteButton.textContent = 'Delete'; deleteButton.onclick = function () { document.getElementById('deleteModalText').textContent = `Are you sure that you want to delete ${printer.name}?`; document.getElementById('deleteModalConfirm').setAttribute('onclick', `deletePrinter(${printer.id})`); } tableData.appendChild(deleteButton); } function addTableData(tableRow, data, id, printer) { const tableData = document.createElement('td'); tableData.id = `printer${printer.id}:${id}`; tableData.textContent = data; tableRow.appendChild(tableData); } function removePrinterFromTable(printerId) { const tableBody = document.getElementById(`printer${printerId}`); tableBody.remove(); }