-
Notifications
You must be signed in to change notification settings - Fork 0
/
students.html
347 lines (300 loc) · 11.1 KB
/
students.html
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Students Database</title>
<link rel="icon" type="image/x-icon" href="./images/favicon/favicon.ico" />
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="./style.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
</head>
<body>
<!------ Main Top ------>
<section class="students-sub-header">
<nav>
<a href="index.html"><img src="images/logo.png" /></a>
<div class="nav-links" id="navLinks">
<i class="fa fa-times" onclick="hideMenu()"></i>
<ul>
<li><a href="index.html">HOME</a></li>
<li><a href="about.html">ABOUT</a></li>
<li><a href="course.html">COURSES</a></li>
<li><a href="blog.html">BLOG</a></li>
<li><a href="contact.html">CONTACT</a></li>
</ul>
</div>
<i class="fa fa-times" onclick="showMenu()"></i>
</nav>
<h1>Admin</h1>
</section>
<!------ Blog Page Content ------>
<section class="blog-content">
<div class="container mt-5">
<h1>Student Database</h1>
<div class="container">
<div id="chartContainer"></div>
</div>
<hr>
<div class="row">
<!-- Add Student Form -->
<form class="col-5" id="studentForm">
<div class="mb-3">
<label for="firstName" class="form-label">First Name</label>
<input type="text" class="form-control" id="firstName" required />
</div>
<div class="mb-3">
<label for="lastName" class="form-label">Last Name</label>
<input type="text" class="form-control" id="lastName" required />
</div>
<div class="mb-3">
<label for="faculty" class="form-label">Faculty</label>
<input type="text" class="form-control" id="faculty" required />
</div>
<div class="mb-3">
<label for="age" class="form-label">Age</label>
<input type="number" class="form-control" id="age" required />
</div>
<div class="mb-3">
<label for="gender" class="form-label">Gender</label>
<select class="form-select" id="gender" required>
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
</div>
<button type="submit" class="btn w-100 btn-success">Save</button>
</form>
<div class="col ms-4 border px-4 rounded">
<!-- Student Table -->
<table class="table mt-4">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Faculty</th>
<th scope="col">Age</th>
<th scope="col">Gender</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody id="studentTableBody"></tbody>
</table>
</div>
</div>
</section>
<script>
// Student data
var students = JSON.parse(localStorage.getItem('students')) || [];
// Function to display student data
function displayStudentData() {
// Retrieve student data from local storage
students = JSON.parse(localStorage.getItem('students')) || [];
// Display student data in the table
// ...
// Generate data for ApexChart
var categories = {};
students.forEach(function(student) {
var category = student.faculty;
var age = parseInt(student.age);
if (!categories[category]) {
categories[category] = {
ages: [],
counts: []
};
}
categories[category].ages.push(age);
});
var series = [];
Object.keys(categories).forEach(function(category) {
var data = categories[category];
var counts = {};
data.ages.forEach(function(age) {
if (!counts[age]) {
counts[age] = 0;
}
counts[age]++;
});
var countArray = Object.values(counts);
series.push(countArray.reduce(function(acc, curr) {
return acc + curr;
}, 0));
});
// Render ApexChart
var options = {
chart: {
type: 'donut',
height: 350
},
series: series,
labels: Object.keys(categories).map(function(category) {
return category.toString();
}),
responsive: [{
breakpoint: 480,
options: {
chart: {
width: 200
},
legend: {
position: 'bottom'
}
}
}]
};
var chart = new ApexCharts(document.getElementById('chartContainer'), options);
chart.render();
}
// Call the displayStudentData function to populate the table and chart initially
displayStudentData();
</script>
<script>
// Function to generate a unique ID
function generateUniqueId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
// Function to save student data
function saveStudentData(event) {
event.preventDefault();
// Retrieve form values
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var faculty = document.getElementById("faculty").value;
var age = document.getElementById("age").value;
var gender = document.getElementById("gender").value;
// Generate unique ID
var id = generateUniqueId();
// Create student object
var student = {
id: id,
firstName: firstName,
lastName: lastName,
faculty: faculty,
age: age,
gender: gender,
};
// Get existing student data from local storage or initialize an empty array
var students = JSON.parse(localStorage.getItem("students")) || [];
// Add new student to the array
students.push(student);
// Save the updated array back to local storage
localStorage.setItem("students", JSON.stringify(students));
// Clear form inputs
document.getElementById("firstName").value = "";
document.getElementById("lastName").value = "";
document.getElementById("faculty").value = "";
document.getElementById("age").value = "";
document.getElementById("gender").value = "";
// Refresh the student table
displayStudentData();
}
// Function to display student data in the table
function displayStudentData() {
var studentTableBody = document.getElementById("studentTableBody");
studentTableBody.innerHTML = "";
// Retrieve student data from local storage
var students = JSON.parse(localStorage.getItem("students")) || [];
students.forEach(function (student) {
var row = document.createElement("tr");
row.innerHTML = `
<td>${student.firstName}</td>
<td>${student.faculty}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
<td>
<button class="btn btn-sm btn-success" onclick="editStudent('${student.id}')">Edit</button>
<button class="btn btn-sm btn-danger" onclick="deleteStudent('${student.id}')"><i class="fa-solid fa-trash"></i></button>
</td>
`;
studentTableBody.appendChild(row);
});
}
// Function to delete a student
function deleteStudent(id) {
// Retrieve student data from local storage
var students = JSON.parse(localStorage.getItem("students")) || [];
// Find the student index
var studentIndex = students.findIndex(function (student) {
return student.id === id;
});
if (studentIndex !== -1) {
// Remove the student from the array
students.splice(studentIndex, 1);
// Save the updated array back to local storage
localStorage.setItem("students", JSON.stringify(students));
// Refresh the student table
displayStudentData();
}
}
// Function to edit a student
function editStudent(id) {
// Retrieve student data from local storage
var students = JSON.parse(localStorage.getItem("students")) || [];
// Find the student by ID
var student = students.find(function (student) {
return student.id === id;
});
if (student) {
// Fill the form with student data
document.getElementById("firstName").value = student.firstName;
document.getElementById("lastName").value = student.lastName;
document.getElementById("faculty").value = student.faculty;
document.getElementById("age").value = student.age;
document.getElementById("gender").value = student.gender;
// Delete the student record
deleteStudent(id);
}
}
// Initial display of student data
displayStudentData();
// Add form submit event listener
var studentForm = document.getElementById("studentForm");
studentForm.addEventListener("submit", saveStudentData);
</script>
<!------ Footer ------>
<section class="footer">
<div>
<hr />
<a
class="text-muted underline"
href="https://github.com/4ourpixels/4ourpixels"
>Website by 4our Pixels</a
>
</div>
</section>
<!------JavaScript for Toggle Menu and Local Storage ------>
<script>
var navLinks = document.getElementById("navLinks");
function showMenu() {
navLinks.style.right = "0";
}
function hideMenu() {
navLinks.style.right = "-200px";
}
</script>
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V"
crossorigin="anonymous"
></script>
</body>
</html>