Skip to content

Commit

Permalink
Request Date sorting (#1041)
Browse files Browse the repository at this point in the history

Co-authored-by: JP Engstrom <jpengstrom@macbook-pro.lan>
  • Loading branch information
jpengst and JP Engstrom committed Aug 16, 2024
1 parent 641da63 commit bb25ad5
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion app/views/permission_requests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<th scope='col' onclick="sortTable(1)">
<p>Call Number <i class="double-arrow"></i></p>
</th>
<th scope='col' onclick="sortTable(2)">
<th scope='col' onclick="sortDate(2)">
<p>Request Date <i class="double-arrow"></i></p>
</th>
<th scope='col' onclick="sortTable(3)">
Expand Down Expand Up @@ -107,4 +107,74 @@
}
}
}

function sortDate(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("permission-requests-table");
switching = true;
// Set the sorting direction to ascending:
dir = "asc";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done:
switching = false;
rows = table.rows;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];

const x_arr = x.innerText.toLowerCase().split("/").reverse()
const y_arr = y.innerText.toLowerCase().split("/").reverse()
const index = [0, 2, 1]
const x_output = index.map(i => x_arr[i]);
const y_output = index.map(i => y_arr[i]);

/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir == "asc") {
// console.log(output.join(""))
// console.log(x.innerText.toLowerCase().split("/").reverse().join("/"))
if (x_output.join("") > y_output.join("")) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
} else if (Number(x_output.join("")) > Number(y_output.join(""))) {
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if (x_output.join("") < y_output.join("")) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
} else if (Number(x_output.join("")) < Number(y_output.join(""))) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
// Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/* If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
</script>

0 comments on commit bb25ad5

Please sign in to comment.