Cs311 Assignment solution 2 by Lost
Cs311 Assignment solution 2 by Lost
Index.html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Managment System</title>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<style>
body {
padding: 20px;
background-color: #f8f9fa;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Management System</title>
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<style>
body {
padding: 20px;
background-color: #f8f9fa;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center mb-4">Employee Management System</h1>
<div class="mb-4">
<button class="btn btn-primary" onclick="displayEmployees()">Display All
Employees</button>
</div>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" id="name" class="form-control" required>
</div>
<div class="mb-3">
<label for="department" class="form-label">Department</label>
<input type="text" id="department" class="form-control" required>
</div>
<div class="mb-3">
<label for="salary" class="form-label">Salary</label>
<input type="number" id="salary" class="form-control" required>
</div>
fetch('employees.xml')
.then(response => response.text())
.then(data => {
const parser = new DOMParser();
xmlDoc = parser.parseFromString(data, "text/xml");
})
.catch(error => console.error('Error loading XML:', error));
function displayEmployees() {
const employees = xmlDoc.getElementsByTagName("Employee");
let output = "<table class='table
table-bordered'><thead><tr><th>ID</th><th>Name</th><th>Department</
th><th>Salary</th></tr></thead><tbody>";
output += <tr><td>${id}</td><td>${name}</td><td>${department}</td><td>$
{salary}</td></tr>;
}
output += "</tbody></table>";
document.getElementById("employeeDetails").innerHTML = output;
}
function addEmployee(event) {
event.preventDefault();
const id = document.getElementById("id").value;
const name = document.getElementById("name").value;
const department = document.getElementById("department").value;
const salary = document.getElementById("salary").value;
newEmployee.appendChild(newId);
newEmployee.appendChild(newName);
newEmployee.appendChild(newDepartment);
newEmployee.appendChild(newSalary);
xmlDoc.getElementsByTagName("Employees")[0].appendChild(newEmployee);
function deleteEmployee(event) {
event.preventDefault();
const id = document.getElementById("deleteId").value;
const employees = xmlDoc.getElementsByTagName("Employee");
for (let i = 0; i < employees.length; i++) {
const employeeId = employees[i].getElementsByTagName("ID")
[0].textContent;
if (employeeId === id) {
employees[i].parentNode.removeChild(employees[i]);
alert("Employee deleted successfully!");
document.getElementById("deleteEmployeeForm").reset();
return;
}
}
</body>
</html>