-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
96 lines (80 loc) · 3.71 KB
/
main.ts
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
import { Course } from './course.js';
import { Student } from './student.js';
import { dataCourses } from './dataCourses.js';
import { dataStudent } from './dataStudent.js';
let coursesTbody: HTMLElement = document.getElementById('courses')!;
const btnfilterByName: HTMLElement = document.getElementById("button-filterByName")!;
const btnfilterByCred: HTMLElement = document.getElementById("button-filterByCredits")!;
const inputSearchBox: HTMLInputElement = <HTMLInputElement> document.getElementById("search-box")!;
const inputSearchMin: HTMLInputElement = <HTMLInputElement> document.getElementById("search-min")!;
const inputSearchMax: HTMLInputElement = <HTMLInputElement> document.getElementById("search-max")!;
const totalCreditElm: HTMLElement = document.getElementById("total-credits")!;
let studentTbody: HTMLElement = document.getElementById('student')!;
btnfilterByName.onclick = () => applyFilterByName();
btnfilterByCred.onclick = () => applyFilterByCredits();
renderCoursesInTable(dataCourses);
renderStudentInTable(dataStudent);
totalCreditElm.innerHTML = `${getTotalCredits(dataCourses)}`
function renderCoursesInTable(courses: Course[]): void {
console.log('Desplegando cursos');
courses.forEach((course) => {
let trElement = document.createElement("tr");
trElement.innerHTML = `<td>${course.name}</td>
<td>${course.professor}</td>
<td>${course.credits}</td>`;
coursesTbody.appendChild(trElement);
});
}
function renderStudentInTable(student: Student): void {
console.log('Desplegando estudiante');
let trElement = document.createElement("tr");
trElement.innerHTML = `<td>Cedula</td> <td>${student.cedula}</td>`;
studentTbody.appendChild(trElement);
let trElement2 = document.createElement("tr");
trElement2.innerHTML = `<td>Código</td> <td>${student.codigo}</td>`;
studentTbody.appendChild(trElement2);
let trElement3 = document.createElement("tr");
trElement3.innerHTML = `<td>Direccion</td> <td>${student.direccion}</td>`;
studentTbody.appendChild(trElement3);
let trElement4 = document.createElement("tr");
trElement4.innerHTML = `<td>Edad</td> <td>${student.edad}</td>`;
studentTbody.appendChild(trElement4);
let trElement5 = document.createElement("tr");
trElement5.innerHTML = `<td>Telefono</td> <td>${student.telefono}</td>`;
studentTbody.appendChild(trElement5);
}
function applyFilterByName() {
let text = inputSearchBox.value;
text = (text == null) ? '' : text;
clearCoursesInTable();
let coursesFiltered: Course[] = searchCourseByName(text, dataCourses);
renderCoursesInTable(coursesFiltered);
}
function searchCourseByName(nameKey: string, courses: Course[]) {
return nameKey === '' ? dataCourses : courses.filter( c =>
c.name.match(nameKey));
}
function applyFilterByCredits() {
let min = Number(inputSearchMin.value);
min = (inputSearchMax.value == '' || isNaN(min)) ? 0 : min;
let max = Number(inputSearchMax.value);
max = (inputSearchMax.value == '' || isNaN(max)) ? 10 : max;
clearCoursesInTable();
let coursesFiltered: Course[] = searchCourseByCredits(min, max, dataCourses);
renderCoursesInTable(coursesFiltered);
}
function searchCourseByCredits(min: number, max: number, courses: Course[]) {
return courses.filter( c => c.credits >= min && c.credits <= max);
}
function getTotalCredits(courses: Course[]): number {
let totalCredits: number = 0;
courses.forEach((course) => totalCredits = totalCredits + course.credits);
return totalCredits;
}
function clearCoursesInTable() {
while (coursesTbody.hasChildNodes()) {
if (coursesTbody.firstChild != null) {
coursesTbody.removeChild(coursesTbody.firstChild);
}
}
}