Skip to content

Commit c18b889

Browse files
committed
Adding Sort Button
Signed-off-by: Lucas Bergholz <[email protected]>
1 parent eb58f90 commit c18b889

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
11 KB
Loading

src/index.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable max-len */
2+
/* eslint-disable no-trailing-spaces */
3+
/* eslint-disable indent */
14
// JS Goes here - ES6 supported
25

36
import "./css/main.css";
@@ -53,6 +56,9 @@ document.addEventListener("DOMContentLoaded", function() {
5356

5457
// Add input for filtering
5558
function addInput(el, label, idx) {
59+
const container = document.createElement('div');
60+
container.setAttribute('style', 'display: flex; align-items: center;');
61+
5662
const input = document.createElement('input');
5763
input.addEventListener("change", function(e) {
5864
widget.filters[idx] = e.target.value;
@@ -65,7 +71,11 @@ document.addEventListener("DOMContentLoaded", function() {
6571
input.setAttribute('type', 'text');
6672
input.setAttribute('name', 'filter_' + label);
6773
input.setAttribute('list', 'opts_for_' + label);
68-
el.appendChild(input);
74+
75+
container.appendChild(input);
76+
addSortButton(container, idx);
77+
78+
el.appendChild(container);
6979
}
7080
const tbody = el.querySelector('tbody');
7181
const headings = Array.from(el.querySelectorAll('thead th')).map((el, idx) => {
@@ -95,6 +105,47 @@ document.addEventListener("DOMContentLoaded", function() {
95105
};
96106
});
97107

108+
function addSortButton(el, idx) {
109+
const img = document.createElement('img');
110+
img.src = "/img/up-down-arrows-icon.png"
111+
img.addEventListener("click", function() {
112+
sortTable(idx);
113+
});
114+
img.setAttribute('style', 'width: 16px; margin-left: 5px' )
115+
116+
el.appendChild(img)
117+
}
118+
119+
let direction = false;
120+
function sortTable(columnIndex) {
121+
removeAllChildNodes(tbody);
122+
if (isNaN(rows[0].columns[columnIndex][0]) && isNaN(rows[rows.length - 1].columns[columnIndex][0])) {
123+
rows.sort((a, b) => {
124+
a = a.columns[columnIndex];
125+
b = b.columns[columnIndex];
126+
return direction ? a.localeCompare(b) : b.localeCompare(a);
127+
});
128+
} else {
129+
if (!columnIndex && window.location.href.includes("docs/alerts")) {
130+
rows.sort((a, b) => {
131+
a = a.columns[columnIndex].split("-");
132+
b = b.columns[columnIndex].split("-");
133+
return direction ? a[0] - b[0] : b[0] - a[0];
134+
});
135+
} else {
136+
rows.sort((a, b) => {
137+
a = a.columns[columnIndex];
138+
b = b.columns[columnIndex];
139+
return direction ? a - b : b - a;
140+
});
141+
}
142+
}
143+
for (let i = 0; i <= rows.length - 1; i++) {
144+
tbody.appendChild(rows[i].el);
145+
}
146+
direction = !direction;
147+
}
148+
98149
// Go through options elements and populate lists with column aggregates
99150
// gathered in previous loop
100151
Object.entries(widget.options).map(pair => {

0 commit comments

Comments
 (0)