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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
| function firstLetter(str) {
if (str == '') return str;
let strOne = str.toLowerCase().trim();
let strTwo = strOne[0].toUpperCase() + strOne.slice(1);
return strTwo;
}
function textYear(yearsOld) {
let txt;
count = yearsOld % 100;
if (count >= 5 && count <= 20) {
txt = 'лет';
} else {
count = count % 10;
if (count == 1) {
txt = 'год';
} else if (count >= 2 && count <= 4) {
txt = 'года';
} else {
txt = 'лет';
}
}
return txt;
}
const buttonSearchStudents = document.querySelector('.search-students');
const buttonAddStudents = document.querySelector('.add-students');
const studentsList = document.querySelector('.students-list');
const searchField = document.querySelector('.search');
const buttonAdd = document.querySelector('.button-add');
buttonAdd.addEventListener('click', function (e) {
e.preventDefault();
fillForms();
createListStudents();
validate();
buttonAdd.disabled = true;
});
function checkYearAdmission() {
const alertYear = document.querySelector('.alert-year');
const alertYear2000 = document.querySelector('.alert-year2000');
const yearAdmission = document.getElementById('year-of-admission');
yearAdmission.addEventListener('input', function () {
if (this.value != this.value.replace(/[^\+\d]/g, '')) {
// (event.keyCode < 48 || event.keyCode > 57)
yearAdmission.disabled = true;
alertYear.style.display = 'block';
yearAdmission.value = '';
alertYear.addEventListener('click', function () {
alertYear.style.display = 'none';
yearAdmission.disabled = false;
});
}
for (let i = 0; i < yearAdmission.value.length; i++) {
if (yearAdmission.value[0] < 2 || yearAdmission.value[1] > 0) {
yearAdmission.disabled = true;
alertYear2000.style.display = 'block';
alertYear2000.addEventListener('click', function () {
alertYear2000.style.display = 'none';
yearAdmission.value = '';
yearAdmission.disabled = false;
});
}
}
});
return yearAdmission;
}
checkYearAdmission();
buttonSearchStudents.addEventListener('click', function () {
searchField.style.display = 'flex';
buttonSearchStudents.style.display = 'none';
});
buttonAddStudents.addEventListener('click', function () {
studentsList.style.display = 'flex';
buttonAddStudents.style.display = 'none';
});
function fillForms() {
const fieldName = document.querySelectorAll('.students-item');
const yearAdmissionStudent = checkYearAdmission();
const surnameStudent = document.getElementById('surname');
const firstnameStudent = document.getElementById('name');
const secondnameStudent = document.getElementById('middlename');
const yearBirthStudent = document.getElementById('year-of-birth');
const facultyStudent = document.getElementById('faculty');
const dataСhecking = document.querySelector('.data-checking');
const studentData = document.querySelector('.wrapper');
const buttonYes = document.querySelector('.consent-button');
let surname = firstLetter(surnameStudent.value);
let firstname = firstLetter(firstnameStudent.value);
let secondname = firstLetter(secondnameStudent.value);
let yearBirth = new Date(yearBirthStudent.value);
let dateBirth = yearBirth.getTime();
let options = {
year: 'numeric',
month: 'long',
day: 'numeric',
};
let dateStr = yearBirth.toLocaleDateString('ru-RU', options);
let today = new Date();
let age = today.getFullYear() - yearBirth.getFullYear();
if (
today.getMonth() < yearBirth.getMonth() ||
(today.getMonth() == yearBirth.getMonth() &&
today.getDate() < yearBirth.getDate())
) {
age--;
}
let yearAdmission = new Date(yearAdmissionStudent.value);
let option = {
year: 'numeric',
};
let dateAdmission = yearAdmission
.toLocaleString('ru', option)
.replace(' г.', '');
let graduation = yearAdmission.getFullYear() + 4;
let course = today.getFullYear() - yearAdmission.getFullYear();
if (
today.getMonth() < 8 &&
today.getFullYear() - yearAdmission.getFullYear() <= 4
) {
course =
dateAdmission +
' ' +
'-' +
' ' +
graduation +
' ' +
course +
' ' +
'курс';
} else {
course =
dateAdmission + ' ' + '-' + ' ' + graduation + ' ' + 'Обучение закончено';
}
let faculty = facultyStudent.options[facultyStudent.selectedIndex].text;
dataСhecking.innerHTML =
surname +
'<br>' +
firstname +
'<br>' +
secondname +
'<br>' +
'Дата рождения' +
' ' +
(dateStr + '<br>' + age + ' ' + textYear(age)) +
'<br>' +
'Годы обучения' +
' ' +
course +
'<br>' +
faculty;
studentData.style.display = 'flex';
buttonYes.style.display = 'block';
return {
surnameStudent,
firstnameStudent,
secondnameStudent,
yearBirthStudent,
surname,
firstname,
secondname,
age,
dateAdmission,
course,
faculty,
yearAdmissionStudent,
studentData,
dataСhecking,
fieldName,
buttonYes,
graduation,
dateBirth,
};
}
function getItemsData() {
let students = JSON.parse(localStorage.getItem('Студенты'));
if (students == undefined) {
students = [];
}
return students;
}
function createListStudents() {
let fillForm = fillForms();
let students = getItemsData();
const consentButton = document.querySelector('.consent-button');
const returnButton = document.querySelector('.return-button');
consentButton.addEventListener('click', function () {
let oneStudent = {};
oneStudent['student'] =
fillForm.surname + ' ' + fillForm.firstname + ' ' + fillForm.secondname;
oneStudent['dateBirth'] = fillForm.dateBirth;
oneStudent['age'] = fillForm.age + ' ' + textYear(fillForm.age);
oneStudent['yearFinish'] = fillForm.graduation;
oneStudent['yearStart'] = fillForm.dateAdmission;
oneStudent['course'] = fillForm.course;
oneStudent['faculty'] = fillForm.faculty;
students.push(oneStudent);
localStorage.setItem('Студенты', JSON.stringify(students));
fillForm.studentData.style.display = 'none';
studentsList.style.display = 'none';
buttonAddStudents.style.display = 'inline-block';
buttonAdd.disabled = false;
fillForm.fieldName.forEach((elem) => {
elem.value = '';
});
createTable();
});
returnButton.addEventListener('click', function () {
for (let i = document.images.length; i--; i > 0)
document.images[i].parentNode.removeChild(document.images[i]);
fillForm.studentData.style.display = 'none';
fillForm.fieldName.forEach((elem) => {
elem.style.border = '1px solid #ced4da';
});
fillForm.studentData.style.backgroundColor = '#efefef';
buttonAdd.disabled = false;
});
}
function letterValidate() {
let fillForm = fillForms();
fillForm.buttonYes.style.display = 'none';
fillForm.studentData.style.backgroundColor = 'transparent';
fillForm.dataСhecking.innerHTML = '';
buttonAdd.disabled = true;
let img = document.createElement('img');
img.classList = 'picture';
img.style.display = 'block';
img.setAttribute('src', 'img/01.png');
fillForm.studentData.append(img);
}
function validate() {
let fillForm = fillForms();
fillForm.fieldName.forEach((elem) => {
if (elem.value == '') {
elem.style.border = '1px solid #dc3545';
letterValidate();
}
});
return true;
}
function createTable() {
let students = getItemsData();
const table = document.getElementById('table-body');
table.innerText = '';
let options = {
year: 'numeric',
month: 'long',
day: 'numeric',
};
for (const key of students) {
let dateBirth = new Date(key.dateBirth).toLocaleString('ru-RU', options);
const td1 = document.createElement('td');
const td2 = document.createElement('td');
const td3 = document.createElement('td');
const td4 = document.createElement('td');
const text1 = document.createTextNode(key.student);
const text2 = document.createTextNode(key.faculty);
const text3 = document.createTextNode(dateBirth + ' ' + key.age);
const text4 = document.createTextNode(key.course);
td3.setAttribute('data-birth', key.dateBirth);
td4.setAttribute('data-admission', key.yearStart);
td4.setAttribute('data-graduation', key.yearFinish);
const tr = document.createElement('tr');
td1.append(text1);
td2.append(text2);
td3.append(text3);
td4.append(text4);
tr.append(td1, td2, td3, td4);
table.append(tr);
}
}
createTable();
const table = document.getElementById('sortable');
const headers = table.querySelectorAll('th');
const directions = Array.from(headers).map((header) => {
return '';
});
function transform(index, cell) {
const type = headers[index].getAttribute('data-name');
if (type === 'age') {
return cell.getAttribute('data-birth') / 10000;
}
if (type === 'year of study') {
return parseFloat(cell.innerHTML);
} else {
return cell.innerHTML;
}
}
function sortColumn(index) {
const tableBody = table.querySelector('tbody');
const rows = tableBody.querySelectorAll('tr');
const direction = directions[index] || 'asc';
const multiplier = direction === 'asc' ? 1 : -1;
const newRows = Array.from(rows);
newRows.sort((rowA, rowB) => {
const cellA = rowA.querySelectorAll('td')[index];
const cellB = rowB.querySelectorAll('td')[index];
const a = transform(index, cellA);
const b = transform(index, cellB);
if (a > b) {
return 1 * multiplier;
}
if (a < b) {
return -1 * multiplier;
}
if (a === b) {
return 0;
}
});
directions[index] = direction === 'asc' ? 'desc' : 'asc';
rows.forEach((row) => {
tableBody.removeChild(row);
});
newRows.forEach((newRow) => {
tableBody.appendChild(newRow);
});
}
headers.forEach((header, index) => {
header.addEventListener('click', function () {
sortColumn(index);
});
});
const buttonSearch = document.querySelector('.button-searh');
buttonSearch.addEventListener('click', (e) => {
e.preventDefault();
tableSearch();
});
function tableSearch() {
let students = getItemsData();
const table = document.getElementById('sortable');
const searchFio = document.getElementById('search-fio');
const searchFaculty = document.getElementById('search-faculty');
const searchYearOfAdmission = document.getElementById(
'search-year-of-admission'
);
const searchYearOfGraduation = document.getElementById(
'search-year-of-graduation'
);
const alertSearch = document.querySelector('.alert-search');
let searchFioPhrase = new RegExp(searchFio.value, 'i');
let searchFacultyPhrase = new RegExp(searchFaculty.value, 'i');
let searchYearOfAdmissionPhrase = searchYearOfAdmission.value;
let searchYearOfGraduationPhrase = searchYearOfGraduation.value;
let hiddeRows = 0;
for (let i = 1; i < table.rows.length; i++) {
let isFound = false;
const regFioText = table.rows[i].cells[0].innerHTML;
const regFacultyText = table.rows[i].cells[1].innerHTML;
const regYearOfAdmissionText = table.rows[i].cells[3].getAttribute(
'data-admission'
);
const regYearOfGraduationText = table.rows[i].cells[3].getAttribute(
'data-graduation'
);
isFound =
(searchFio.value == '' || searchFioPhrase.test(regFioText)) &&
(searchFaculty.value == '' || searchFacultyPhrase.test(regFacultyText)) &&
(searchYearOfAdmission.value == '' ||
searchYearOfAdmissionPhrase === regYearOfAdmissionText) &&
(searchYearOfGraduation.value == '' ||
searchYearOfGraduationPhrase === regYearOfGraduationText);
if (isFound) {
table.rows[i].style.display = '';
// table.rows[i].classList.add("table-success")
} else {
table.rows[i].style.display = 'none';
if (table.rows[i].style.display == 'none') hiddeRows++;
if (table.rows.length - 1 == hiddeRows) {
alertSearch.style.display = 'block';
alertSearch.addEventListener('click', function () {
alertSearch.style.display = 'none';
searchFio.value = '';
searchFaculty.value = '';
searchYearOfAdmission.value = '';
searchYearOfGraduation.value = '';
searchField.style.display = 'none';
buttonSearchStudents.style.display = 'inline-block';
createTable();
});
}
}
}
} |