-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweekajax.js
101 lines (83 loc) · 3.1 KB
/
weekajax.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var mainForm, filter, dag, week, jaar;
var aanwezigheidsLijst;
function init() {
mainForm = document.getElementById("weekform");
filter = document.getElementById("filter");
dag = document.getElementById("dag");
week = document.getElementById("week");
jaar = document.getElementById("jaar");
filter.addEventListener("keyup", function(e) { filterName(e); });
dag.addEventListener("change", function(e) { filterName(e); });
week.addEventListener("change", function(e) { filterName(e); });
jaar.addEventListener("change", function(e) { filterName(e); });
filter.focus();
}
function initAanwezigheidsLijst() {
aanwezigheidsLijst = document.getElementById("aanwezigheid");
if (aanwezigheidsLijst != null) {
var forms = aanwezigheidsLijst.querySelectorAll("form");
[].forEach.call(forms, function(form) {
initCheckboxListeners(form);
});
}
}
function initCheckboxListeners(form) {
var elements = form.elements;
console.log(elements);
[].forEach.call(elements, function(element) {
if (element.type == "checkbox") {
element.addEventListener("change", function(e) { checkboxHandler(e); });
}
})
}
function filterName(e) {
e.preventDefault();
var formData = new FormData(mainForm);
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if(document.getElementById('aanwezigheid') != null) {
mainForm.parentNode.removeChild(document.getElementById('aanwezigheid'));
mainForm.parentNode.appendChild(xmlhttp.responseXML.getElementById('aanwezigheid'));
} else {
mainForm.parentNode.appendChild(xmlhttp.responseXML.getElementById('aanwezigheid'));
}
initAanwezigheidsLijst();
}
}
xmlhttp.responseType = 'document';
xmlhttp.open("POST", mainForm.action, true);
history.pushState(null, null, mainForm.action);
xmlhttp.send(formData);
}
function checkboxHandler(e) {
e.preventDefault();
var form = e.target.form;
var formRow = form.parentNode;
var formData = new FormData(form);
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
while (formRow.firstChild) {
formRow.removeChild(formRow.firstChild);
}
formRow.innerHTML = "<td colspan='6'><center>loading</center></td>";
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var newFormRow = xmlhttp.responseXML.getElementById(form.getAttribute("id")).parentNode;
var nextSibling = formRow.nextSibling;
var parentNode = formRow.parentNode;
parentNode.removeChild(formRow);
if (nextSibling == null) {
parentNode.appendChild(newFormRow);
} else {
parentNode.insertBefore(newFormRow, nextSibling);
}
initAanwezigheidsLijst();
}
}
xmlhttp.responseType = 'document';
xmlhttp.open("POST", mainForm.action, true);
history.pushState(null, null, mainForm.action);
xmlhttp.send(formData);
}
window.addEventListener("load", init);
initAanwezigheidsLijst();