Skip to content

Commit

Permalink
fix: Fixing sort and filter so they work at the same time
Browse files Browse the repository at this point in the history
Co-authored-by: Ana Rocha <[email protected]>
Signed-off-by: Lucas Bergholz <[email protected]>
  • Loading branch information
LucasBergholz and anaaroch committed Aug 27, 2024
1 parent c18b889 commit c8f7104
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -104,6 +107,7 @@ document.addEventListener("DOMContentLoaded", function() {
columns, // Needed for filtered
};
});
let elements = rows;

function addSortButton(el, idx) {
const img = document.createElement('img');
Expand All @@ -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;
}
Expand Down

0 comments on commit c8f7104

Please sign in to comment.