test/public/js/tableActions.js

79 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-02-01 10:22:12 +00:00
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);
2024-02-01 12:12:19 +00:00
addTableData(tableRow, `${printer.max_speed}mm/s`, 'max_speed', printer);
2024-02-01 10:22:12 +00:00
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();
}