-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
307 lines (272 loc) · 8.75 KB
/
script.js
File metadata and controls
307 lines (272 loc) · 8.75 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Sample initial data
let students = [
{
id: 1,
name: "Rahul Sharma",
email: "rahul.sharma@example.com",
mobile: "9876543210",
branch: "Computer Science",
course: "B.Tech",
batch: "2021-2025",
},
{
id: 2,
name: "Priya Patel",
email: "priya.patel@example.com",
mobile: "8765432109",
branch: "Electrical Engineering",
course: "M.Tech",
batch: "2020-2022",
},
{
id: 3,
name: "Amit Singh",
email: "amit.singh@example.com",
mobile: "7654321098",
branch: "Mechanical Engineering",
course: "B.Tech",
batch: "2022-2026",
},
];
// DOM elements
const addStudentBtn = document.getElementById("add-student-btn");
const addStudentForm = document.getElementById("add-student-form");
const saveStudentBtn = document.getElementById("save-student");
const cancelAddBtn = document.getElementById("cancel-add");
const searchInput = document.getElementById("search-input");
const searchBtn = document.getElementById("search-btn");
const resetSearchBtn = document.getElementById("reset-search");
const studentsContainer = document.getElementById("students-container");
const filterBranch = document.getElementById("filter-branch");
const filterCourse = document.getElementById("filter-course");
const filterBatch = document.getElementById("filter-batch");
const applyFilterBtn = document.getElementById("apply-filter");
const resetFilterBtn = document.getElementById("reset-filter");
// Stats elements
const totalStudentsEl = document.getElementById("total-students");
const csStudentsEl = document.getElementById("cs-students");
const eeStudentsEl = document.getElementById("ee-students");
const mechStudentsEl = document.getElementById("mech-students");
// Form fields
const nameInput = document.getElementById("name");
const emailInput = document.getElementById("email");
const mobileInput = document.getElementById("mobile");
const branchInput = document.getElementById("branch");
const courseInput = document.getElementById("course");
const batchInput = document.getElementById("batch");
// Current state
let isEditing = false;
let currentEditId = null;
let filteredStudents = [...students];
// Initialize the app
function init() {
renderStudents();
updateStats();
// Event listeners
addStudentBtn.addEventListener("click", () => {
addStudentForm.style.display = "block";
resetForm();
isEditing = false;
});
cancelAddBtn.addEventListener("click", () => {
addStudentForm.style.display = "none";
resetForm();
});
saveStudentBtn.addEventListener("click", saveStudent);
searchBtn.addEventListener("click", searchStudents);
resetSearchBtn.addEventListener("click", resetSearch);
applyFilterBtn.addEventListener("click", applyFilters);
resetFilterBtn.addEventListener("click", resetFilters);
}
// Render students to the DOM
function renderStudents() {
studentsContainer.innerHTML = "";
if (filteredStudents.length === 0) {
studentsContainer.innerHTML =
"<p>No students found matching your criteria.</p>";
return;
}
filteredStudents.forEach((student) => {
const studentCard = document.createElement("div");
studentCard.className = "student-card";
studentCard.innerHTML = `
<div class="student-header">
<div class="student-name">${student.name}</div>
<div class="student-id">#${student.id}</div>
</div>
<div class="student-details">
<div>
<div class="detail-label">Email</div>
<div>${student.email}</div>
</div>
<div>
<div class="detail-label">Mobile</div>
<div>${student.mobile}</div>
</div>
<div>
<div class="detail-label">Branch</div>
<div>${student.branch}</div>
</div>
<div>
<div class="detail-label">Course</div>
<div>${student.course}</div>
</div>
<div>
<div class="detail-label">Batch</div>
<div>${student.batch}</div>
</div>
</div>
<div class="action-buttons">
<button class="edit-btn" data-id="${student.id}">Edit</button>
<button class="delete-btn" data-id="${student.id}">Delete</button>
</div>
`;
studentsContainer.appendChild(studentCard);
});
// Add event listeners to edit and delete buttons
document.querySelectorAll(".edit-btn").forEach((btn) => {
btn.addEventListener("click", (e) => {
const id = parseInt(e.target.getAttribute("data-id"));
editStudent(id);
});
});
document.querySelectorAll(".delete-btn").forEach((btn) => {
btn.addEventListener("click", (e) => {
const id = parseInt(e.target.getAttribute("data-id"));
deleteStudent(id);
});
});
}
// Save student (add or update)
function saveStudent() {
// Validate form
if (
!nameInput.value ||
!emailInput.value ||
!mobileInput.value ||
!branchInput.value ||
!courseInput.value ||
!batchInput.value
) {
alert("Please fill all fields");
return;
}
const studentData = {
name: nameInput.value,
email: emailInput.value,
mobile: mobileInput.value,
branch: branchInput.value,
course: courseInput.value,
batch: batchInput.value,
};
if (isEditing) {
// Update existing student
const index = students.findIndex((s) => s.id === currentEditId);
if (index !== -1) {
students[index] = { ...students[index], ...studentData };
}
} else {
// Add new student
const newId =
students.length > 0 ? Math.max(...students.map((s) => s.id)) + 1 : 1;
students.push({ id: newId, ...studentData });
}
// Reset and hide form
addStudentForm.style.display = "none";
resetForm();
// Update UI
applyFilters(); // This will re-render with current filters
updateStats();
}
// Edit student
function editStudent(id) {
const student = students.find((s) => s.id === id);
if (!student) return;
// Fill form with student data
nameInput.value = student.name;
emailInput.value = student.email;
mobileInput.value = student.mobile;
branchInput.value = student.branch;
courseInput.value = student.course;
batchInput.value = student.batch;
// Set editing state
isEditing = true;
currentEditId = id;
addStudentForm.style.display = "block";
// Scroll to form
addStudentForm.scrollIntoView({ behavior: "smooth" });
}
// Delete student
function deleteStudent(id) {
if (confirm("Are you sure you want to delete this student?")) {
students = students.filter((s) => s.id !== id);
applyFilters(); // This will re-render with current filters
updateStats();
}
}
// Search students by name
function searchStudents() {
const searchTerm = searchInput.value.toLowerCase();
if (!searchTerm) {
filteredStudents = [...students];
} else {
filteredStudents = students.filter((student) =>
student.name.toLowerCase().includes(searchTerm)
);
}
renderStudents();
}
// Reset search
function resetSearch() {
searchInput.value = "";
filteredStudents = [...students];
renderStudents();
}
// Apply filters
function applyFilters() {
const branch = filterBranch.value;
const course = filterCourse.value;
const batch = filterBatch.value;
filteredStudents = students.filter((student) => {
return (
(!branch || student.branch === branch) &&
(!course || student.course === course) &&
(!batch || student.batch.includes(batch))
);
});
renderStudents();
}
// Reset filters
function resetFilters() {
filterBranch.value = "";
filterCourse.value = "";
filterBatch.value = "";
filteredStudents = [...students];
renderStudents();
}
// Reset form
function resetForm() {
nameInput.value = "";
emailInput.value = "";
mobileInput.value = "";
branchInput.value = "";
courseInput.value = "";
batchInput.value = "";
isEditing = false;
currentEditId = null;
}
// Update statistics
function updateStats() {
totalStudentsEl.textContent = students.length;
csStudentsEl.textContent = students.filter(
(s) => s.branch === "Computer Science"
).length;
eeStudentsEl.textContent = students.filter(
(s) => s.branch === "Electrical Engineering"
).length;
mechStudentsEl.textContent = students.filter(
(s) => s.branch === "Mechanical Engineering"
).length;
}
// Initialize the application
document.addEventListener("DOMContentLoaded", init);