-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateElements.js
77 lines (75 loc) · 2.62 KB
/
createElements.js
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
function emptyResult() {
const resultElement = document.getElementById('result');
resultElement.innerHTML = '<p>0 results for the search.</p>';
}
function updateTable(employees, roles) {
//If employees json is empty
if (employees.length == 0) {
emptyResult();
return;
}
//If has employees on json
else {
const resultElement = document.getElementById('result');
resultElement.innerHTML = '';
const tableElement = document.createElement('table');
resultElement.appendChild(tableElement);
//Head
const theadElement = document.createElement('thead');
tableElement.appendChild(theadElement);
const trHeadElement = document.createElement('tr');
theadElement.appendChild(trHeadElement);
['ID', 'Name', 'Role', 'Salary (R$)'].map((element) => {
const thElement = document.createElement('th');
thElement.innerHTML = element;
trHeadElement.appendChild(thElement);
});
//Body
const tbodyElement = document.createElement('tbody');
tableElement.appendChild(tbodyElement);
employees.map((element) => {
const trBodyElement = document.createElement('tr');
tbodyElement.appendChild(trBodyElement);
[element.id, element.name, element.role_id, element.salary].map(
(field, index) => {
let value = field;
if (index == 2) {
value = roles
.filter((r) => r.id == element.role_id)
.map((r) => r.name)[0];
} else if (index == 3) {
value = parseFloat(value).toFixed(2);
}
const tdElement = document.createElement('td');
tdElement.innerHTML = value;
trBodyElement.appendChild(tdElement);
}
);
});
}
//Span count
const countElement = document.getElementById('count');
countElement.innerHTML = employees.length;
}
function updateFilter(roles) {
const filterByRolesElement = document.getElementById('filterByRoles');
roles.map((element) => {
//Div
const divElement = document.createElement('div');
filterByRolesElement.appendChild(divElement);
//Checkbox
const checkboxElement = document.createElement('input');
checkboxElement.setAttribute('type', 'checkbox');
checkboxElement.setAttribute('value', element.id);
checkboxElement.setAttribute('name', 'checkboxFilter');
divElement.appendChild(checkboxElement);
//Label
const labelElement = document.createElement('label');
labelElement.innerHTML = element.name;
divElement.appendChild(labelElement);
//Id
const id = 'checkbox' + element.id;
checkboxElement.id = id;
labelElement.setAttribute('for', id);
});
}