-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable-filtering.js
225 lines (183 loc) · 6.13 KB
/
table-filtering.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
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
(function () {
var table = document.querySelector('.my-work')
var rows = table.querySelectorAll('tbody');
var page = 0;
var rpp = 10;
var pagination = document.getElementById('pagination')
function filterTable(value, columns, classesToHide) {
[].forEach.call(rows, function(row, index) {
var hideRow = true
var cells = row.querySelectorAll('td')
columns.forEach(function(columnIndex) {
// value might be an array of values...
if( Array.isArray(value)) {
value.forEach(function(item) {
if ( cells[columnIndex].textContent.toLowerCase().indexOf(item.toLowerCase()) !== -1 ) {
hideRow = false
}
})
} else {
if ( cells[columnIndex].textContent.toLowerCase().indexOf(value.toLowerCase()) !== -1 ) {
hideRow = false
}
}
})
if (hideRow) {
row.classList.add('govuk-visually-hidden')
}
})
}
function resetTable() {
[].forEach.call(rows, function(row, index) {
row.classList.remove('govuk-visually-hidden')
})
}
// function hideViewedRows() {
// [].forEach.call(rows, function(row, index) {
// if (row.classList.contains('viewed')) {
// row.classList.add('govuk-visually-hidden')
// }
// })
// }
// Bind filtering functionality to search box
var searchBox = document.getElementById('search-input')
var applicationType = document.getElementById('application-type')
var applicationProgress = document.getElementById('application-progress')
var searchForm = document.getElementById('search')
searchForm.addEventListener('submit', function(e) {
e.preventDefault()
updateSearch()
})
// Change handler on the checkboxes
applicationProgressCheckboxes = Array.prototype.slice.call(document.querySelectorAll('[name="application-progress"]'));
applicationTypeCheckboxes = Array.prototype.slice.call(document.querySelectorAll('[name="application-type"]'));
applicationProgressCheckboxes.concat(applicationTypeCheckboxes).forEach(function(checkbox, index) {
checkbox.addEventListener('change', function(e) {
updateSearch()
})
})
function getSelectedCheckboxValues(checkboxes) {
return checkboxes.reduce(function(accumulator, currentCheckbox, currentIndex, array) {
if(currentCheckbox.checked) {
return accumulator.concat(currentCheckbox.value)
} else {
return accumulator
}
}, [])
}
function updateSearch() {
resetTable()
const applicationProgressChoices = getSelectedCheckboxValues(applicationProgressCheckboxes)
// if (applicationProgressChoices.length > 0) {
filterTable(applicationProgressChoices, [5])
// }
const applicationTypeChoices = getSelectedCheckboxValues(applicationTypeCheckboxes)
if (applicationTypeChoices.length > 0) {
filterTable(applicationTypeChoices, [3])
} else {
filterTable(applicationTypeChoices, [0])
}
if(searchBox.value.length > 0) {
filterTable(searchBox.value, [0, 1])
}
// // // paginate
// resultSet = Array.prototype.slice.call(rows).filter(function(row) {
// return !row.classList.contains('govuk-visually-hidden')
// })
//
// // Only paginate if we've got enough
// if(resultSet.length > rpp) {
// // Find the rows that are not on the current page, and hide them
// previousPageRows = resultSet.slice(0, page * rpp)
// subsequentPageRows = resultSet.slice((page + 1) * rpp)
//
//
// previousPageRows.concat(subsequentPageRows).forEach(function(row) {
// row.classList.add('govuk-visually-hidden')
// })
// }
//
// renderPagination(resultSet)
// if (hideViewed.checked) {
// hideViewedRows()
// }
}
// Do an initial search on page load
updateSearch()
// Reset link
document.getElementById('reset')
reset.addEventListener('click', function(e) {
e.preventDefault()
resetTable()
searchBox.value = ''
applicationTypeCheckboxes.concat(applicationProgressCheckboxes).forEach(function(checkbox) {
checkbox.checked = true
})
// hideViewed.checked = false
})
// // Pagination control
// function renderPagination(resultSet) {
//
// var totalPages = Math.ceil(resultSet.length / rpp)
// pagination.innerHTML = ''
// if(totalPages > 1) {
// var link
// for(i=0; i<totalPages; i++) {
// link = document.createElement('a')
// link.textContent = i + 1
// link.href = '#'
// link.style.marginRight = '10px'
// link.setAttribute('data-page', i)
// link.addEventListener('click', function(e) {
// e.preventDefault()
// page = parseInt(this.getAttribute('data-page'))
// updateSearch()
// })
// pagination.appendChild(link)
// }
// }
// }
// select all //
$('#select-all').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$('.checkbox-progress input[type="checkbox"]').each(function() {
this.checked = true;
});
} else {
$('.checkbox-progress input[type="checkbox"]').each(function() {
this.checked = false;
});
}
updateSearch();
});
// select all session C//
$('#select-all').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$('.checkbox-progress2 input[type="checkbox"]').each(function() {
this.checked = true;
});
} else {
$('.checkbox-progress2 input[type="checkbox"]').each(function() {
this.checked = false;
});
}
updateSearch();
});
})()
// var coll = document.getElementsByClassName("filter");
// var i;
//
// for (i = 0; i < coll.length; i++) {
// coll[i].addEventListener("click", function() {
// this.classList.toggle("active");
// var content = this.nextElementSibling;
// if (content.style.display === "block") {
// content.style.display = "none";
// } else {
// content.style.display = "block";
// }
// });
// }
// table filter icons //