2023-11-17 16:55:32 +04:00

51 lines
1.5 KiB
JavaScript

export const cntrls = {
button: document.getElementById("user-add"),
table: document.querySelector("#users-table tbody"),
form: document.getElementById("users-form"),
userId: document.getElementById("user-id"),
handle: document.getElementById("handle"),
email: document.getElementById("email"),
password: document.getElementById("password")
};
function createTableAnchor(icon, callback) {
const i = document.createElement("i");
i.classList.add("fa-solid", icon);
const a = document.createElement("a");
a.href = "#";
a.appendChild(i);
a.onclick = (event) => {
event.preventDefault();
event.stopPropagation();
callback();
};
const td = document.createElement("td");
td.appendChild(a);
return td;
}
function createTableColumn(value) {
const td = document.createElement("td");
td.textContent = value;
return td;
}
export function createTableRow(user, index, editCallback, deleteCallback) {
const rowNumber = document.createElement("th");
rowNumber.scope = "row";
rowNumber.textContent = index + 1;
const row = document.createElement("tr");
row.id = `line-${user.id}`;
row.appendChild(rowNumber);
row.appendChild(createTableColumn(user.handle));
row.appendChild(createTableColumn(user.email));
row.appendChild(createTableColumn(user.password));
row.appendChild(createTableAnchor("fa-pencil", editCallback));
row.appendChild(createTableAnchor("fa-trash", deleteCallback));
return row;
}