UI: Improve performance of the quick filter

This commit is contained in:
kkthxbye 2021-11-25 12:14:07 +01:00
parent 86ada33577
commit a0b9ac7bcc
3 changed files with 16 additions and 6 deletions

Binary file not shown.

Binary file not shown.

View File

@ -107,6 +107,9 @@ function initTableFilter(): void {
// Create a regex pattern from the input search text to match against. // Create a regex pattern from the input search text to match against.
const filter = new RegExp(target.value.toLowerCase().trim()); const filter = new RegExp(target.value.toLowerCase().trim());
// List of which rows which match the query
const matchedRows: Array<HTMLTableRowElement> = [];
for (const row of rows) { for (const row of rows) {
// Find the row's checkbox and deselect it, so that it is not accidentally included in form // Find the row's checkbox and deselect it, so that it is not accidentally included in form
// submissions. // submissions.
@ -114,19 +117,26 @@ function initTableFilter(): void {
if (checkBox !== null) { if (checkBox !== null) {
checkBox.checked = false; checkBox.checked = false;
} }
// Iterate through each row's cell values // Iterate through each row's cell values
for (const value of getRowValues(row)) { for (const value of getRowValues(row)) {
if (filter.test(value.toLowerCase())) { if (filter.test(value.toLowerCase())) {
// If this row matches the search pattern, but is already hidden, unhide it and stop // If this row matches the search pattern, add it to the list.
// iterating through the rest of the cells. matchedRows.push(row);
row.classList.remove('d-none');
break; break;
} else {
// If none of the cells in this row match the search pattern, hide the row.
row.classList.add('d-none');
} }
} }
} }
// Iterate the rows again to set visibility.
// This results in a single reflow instead of one for each row.
for (const row of rows) {
if (matchedRows.indexOf(row) >= 0) {
row.classList.remove('d-none');
} else {
row.classList.add('d-none');
}
}
} }
input.addEventListener('keyup', debounce(handleInput, 300)); input.addEventListener('keyup', debounce(handleInput, 300));
} }