Repository navigation
Expand file tree
/
Copy pathjavascript.js
More file actions
79 lines (69 loc) · 2.77 KB
/
Copy pathjavascript.js
File metadata and controls
79 lines (69 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
document.addEventListener("DOMContentLoaded", function () {
const employeeForm = document.getElementById("employeeForm");
const employeeTableBody = document.getElementById("employeeTableBody");
let employees = [];
let editIndex = -1;
function renderTable() {
employeeTableBody.innerHTML = "";
employees.forEach((employee, index) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${employee.name}</td>
<td>${employee.email}</td>
<td>${employee.mobile}</td>
<td>${employee.position}</td>
<td>
<button class="btn btn-warning btn-sm" onclick="editEmployee(${index})">Edit</button>
<button class="btn btn-danger btn-sm" onclick="deleteEmployee(${index})">Delete</button>
</td>
`;
employeeTableBody.appendChild(row);
});
}
function resetForm() {
employeeForm.reset();
editIndex = -1;
}
window.editEmployee = function (index) {
const employee = employees[index];
document.getElementById("employeeName").value = employee.name;
document.getElementById("employeeEmail").value = employee.email;
document.getElementById("employeeMobile").value = employee.mobile;
document.getElementById("employeePosition").value = employee.position;
document.getElementById("employeeIndex").value = index;
editIndex = index;
};
window.deleteEmployee = function (index) {
if (confirm("Are you sure you want to delete this employee?")) {
employees.splice(index, 1);
renderTable();
}
};
employeeForm.addEventListener("submit", function (event) {
event.preventDefault();
const employeeName = document.getElementById("employeeName").value.trim();
const employeeEmail = document.getElementById("employeeEmail").value.trim();
const employeeMobile = document.getElementById("employeeMobile").value.trim();
const employeePosition = document.getElementById("employeePosition").value.trim();
if (employeeForm.checkValidity() === false) {
event.stopPropagation();
employeeForm.classList.add("was-validated");
return;
}
const newEmployee = {
name: employeeName,
email: employeeEmail,
mobile: employeeMobile,
position: employeePosition,
};
if (editIndex >= 0) {
employees[editIndex] = newEmployee;
editIndex = -1;
} else {
employees.push(newEmployee);
}
renderTable();
resetForm();
employeeForm.classList.remove("was-validated");
});
});