NodeJS CRUD
NodeJS CRUD
Configs:
Student information: _id, studentId, name, dob, address
const apiUrl = 'http://localhost:3001'
Steps:
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"
/>
<title>Student CRUD</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<script src="./script.js"></script>
</body>
</html>
<table id="studentTable">
<thead>
<tr>
<th style="display: none">Database Entry Id</th>
<th>Student ID</th>
<th>Name</th>
<th>Date of Birth</th>
<th>Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="studentList">
<!-- Rows will be dynamically added here -->
</tbody>
</table>
<div id="paginationControls" class="pagination-controls"></div>
</div>
function displayStudents() {
const studentTableBody = document.getElementById('studentList');
studentTableBody.innerHTML = ''; // Clear existing data
paginatedStudents.forEach(student => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td style="display: none">${student._id}</td>
<td>${student.studentId}</td>
<td>${student.name}</td>
<td>${student.dob.slice(0, 10)}</td>
<td>${student.address}</td>
<td>
<button class="edit" onclick="loadStudent('$
{student._id}')">Edit</button>
<button class="delete" onclick="deleteStudent('$
{student._id}')">Delete</button>
</td>
`;
studentTableBody.appendChild(tr);
});
}
Pagination
// Setup pagination controls
function setupPagination() {
const paginationControls =
document.getElementById('paginationControls');
paginationControls.innerHTML = ''; // Clear existing pagination buttons
const totalPages = Math.ceil(students.length / pageSize);
button.addEventListener('click', () => {
currentPage = i;
displayStudents(); // Display the students for the selected
page
});
paginationControls.appendChild(button);
}
}
// Add or update student
document.getElementById('studentForm').addEventListener('submit', async
(event) => {
event.preventDefault();
console.log(document.getElementById('update'))
const isUpdate = event.submitter.id != "addSubmit"
const method = isUpdate ? 'PUT' : 'POST'
let url = apiUrl
const studentData = {
dbEntryId: document.getElementById('dbEntryId').value,
studentId: document.getElementById('studentId').value,
name: document.getElementById('name').value,
dob: document.getElementById('dob').value,
address: document.getElementById('address').value
};
try {
const response = await fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(studentData)
});
if (response.ok) {
hidePopup(); // Hide the popup after saving
await fetchStudents();
} else {
const errorResponse = await response.json();
alert("Error: ", errorResponse)
}
} catch (error) {
console.error('Error adding/updating student: ', error)
}
});
// Event listener for the "Add Student" button to show the popup
document.getElementById('addStudentBtn').addEventListener('click', () => {
showPopup(false) });
// Delete student
async function deleteStudent(id) {
await fetch(`${apiUrl}/${id}`, { method: 'DELETE' });
await fetchStudents();
}
// Initial fetch of students
fetchStudents();
/* Modal Styling */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
form#studentForm button[type="submit"] {
display: none;
}
- Class container:
.container {
width: 95%;
margin: auto;
}
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid #ddd;
}