From c8f7104215c6be3abed0c525c3a8459e606a920f Mon Sep 17 00:00:00 2001 From: Lucas Bergholz Date: Mon, 26 Aug 2024 21:06:38 -0300 Subject: [PATCH] fix: Fixing sort and filter so they work at the same time Co-authored-by: Ana Rocha Signed-off-by: Lucas Bergholz --- src/index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 3c02f59e4..a8ae7201c 100644 --- a/src/index.js +++ b/src/index.js @@ -63,9 +63,12 @@ document.addEventListener("DOMContentLoaded", function() { input.addEventListener("change", function(e) { widget.filters[idx] = e.target.value; removeAllChildNodes(tbody); + elements = []; rows.filter(isFilterMatch).map(r => { tbody.appendChild(r.el) + elements.push(r) }); + console.log(elements); }); input.setAttribute('style', 'width:100%;display:block') input.setAttribute('type', 'text'); @@ -104,6 +107,7 @@ document.addEventListener("DOMContentLoaded", function() { columns, // Needed for filtered }; }); + let elements = rows; function addSortButton(el, idx) { const img = document.createElement('img'); @@ -118,30 +122,31 @@ document.addEventListener("DOMContentLoaded", function() { let direction = false; function sortTable(columnIndex) { + console.log(elements); removeAllChildNodes(tbody); - if (isNaN(rows[0].columns[columnIndex][0]) && isNaN(rows[rows.length - 1].columns[columnIndex][0])) { - rows.sort((a, b) => { + if (isNaN(elements[0].columns[columnIndex][0]) && isNaN(elements[elements.length - 1].columns[columnIndex][0])) { + elements.sort((a, b) => { a = a.columns[columnIndex]; b = b.columns[columnIndex]; return direction ? a.localeCompare(b) : b.localeCompare(a); }); } else { if (!columnIndex && window.location.href.includes("docs/alerts")) { - rows.sort((a, b) => { + elements.sort((a, b) => { a = a.columns[columnIndex].split("-"); b = b.columns[columnIndex].split("-"); return direction ? a[0] - b[0] : b[0] - a[0]; }); } else { - rows.sort((a, b) => { + elements.sort((a, b) => { a = a.columns[columnIndex]; b = b.columns[columnIndex]; return direction ? a - b : b - a; }); } } - for (let i = 0; i <= rows.length - 1; i++) { - tbody.appendChild(rows[i].el); + for (let i = 0; i <= elements.length - 1; i++) { + tbody.appendChild(elements[i].el); } direction = !direction; }