write a code for hrms using html,css and javascript
- User
write a code for hrms using html,css and javascript
Assistant
write a code for hrms using html,css and javascript
Okay, let's craft a basic HRMS (Human Resources Management System) using HTML, CSS, and JavaScript. This will be a simplified version, focusing on core features like employee listing, adding new employees, and basic employee details. I'll break it down into HTML structure, CSS styling, and JavaScript functionality.
Important Considerations:
localStorage for data storage. This is suitable for simple demos, but for a real application, you'd use a database (e.g., MySQL, PostgreSQL, MongoDB).1. HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HRMS - Employee Management</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Employee Management</h1>
<div class="add-employee">
<h2>Add New Employee</h2>
<form id="addEmployeeForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="department">Department:</label>
<input type="text" id="department" name="department" required><br><br>
<button type="submit">Add Employee</button>
</form>
</div>
<div class="employee-list">
<h2>Employee List</h2>
<ul id="employeeList">
<!-- Employee entries will be added here -->
</ul>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Explanation:
<!DOCTYPE html>: Declares the document type.<head>: Contains metadata (title, character set, link to CSS).<body>: The visible content of the page.<div class="container">: A container for the entire layout.<h1>: The main heading.<div class="add-employee">: A section for adding new employees.
<form>: The form for adding employee details.<label>: Labels for input fields.<input>: Input fields for name, email, and department. required attribute makes fields mandatory.<button>: The submit button.<div class="employee-list">: A section to display the employee list.
<ul>: An unordered list to hold employee entries. The employee entries will be dynamically added to this list using JavaScript.<script src="script.js"></script>: Links the JavaScript file.2. CSS (style.css)
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
}
.container {
width: 80%;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1, h2 {
text-align: center;
margin-bottom: 20px;
}
.add-employee {
border: 1px solid #ccc;
padding: 15px;
margin-bottom: 20px;
border-radius: 5px;
}
.add-employee label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.add-employee input[type="text"],
.add-employee input[type="email"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.employee-list {
border: 1px solid #ccc;
padding: 15px;
border-radius: 5px;
}
#employeeList {
list-style-type: none;
padding: 0;
}
#employeeList li {
padding: 10px;
border-bottom: 1px solid #eee;
}
#employeeList li:last-child {
border-bottom: none;
}
Explanation:
font-family, background-color, margin, padding, border, and border-radius to create a visually appealing layout.3. JavaScript (script.js)
// Data storage (using localStorage)
const employeeKey = 'employees';
// Function to load employees from localStorage
function loadEmployees() {
const storedEmployees = localStorage.getItem(employeeKey);
return storedEmployees ? JSON.parse(storedEmployees) : [];
}
// Function to save employees to localStorage
function saveEmployees(employees) {
localStorage.setItem(employeeKey, JSON.stringify(employees));
}
// Function to add a new employee
function addEmployee() {
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const department = document.getElementById('department').value;
if (!name || !email || !department) {
alert('Please fill in all fields.');
return;
}
const employees = loadEmployees();
const newEmployee = {
id: Date.now(), // Generate a unique ID
name: name,
email: email,
department: department
};
employees.push(newEmployee);
saveEmployees(employees);
// Clear the form
document.getElementById('addEmployeeForm').reset();
// Update the employee list
updateEmployeeList(employees);
}
// Function to update the employee list
function updateEmployeeList(employees) {
const employeeList = document.getElementById('employeeList');
employeeList.innerHTML = ''; // Clear the existing list
employees.forEach(employee => {
const listItem = document.createElement('li');
listItem.textContent = `${employee.name} (${employee.email}) - ${employee.department}`;
employeeList.appendChild(listItem);
});
}
// Event listener for the form submission
document.getElementById('addEmployeeForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
addEmployee();
});
// Initial load of employees
window.addEventListener('load', function() {
const employees = loadEmployees();
updateEmployeeList(employees);
});
Explanation:
employeeKey: A constant to store the key used for saving employee data in localStorage.loadEmployees(): Retrieves employee data from localStorage. If no data exists, it returns an empty array.saveEmployees(): Saves the employee data to localStorage as a JSON string.addEmployee():
employees array.localStorage.updateEmployeeList():
employees array and creates a list item (<li>) for each employee.<ul> element.submit event. When the form is submitted, the addEmployee() function is