Add numeric sort for column Users

pull/7/head
mdPlusPlus 2 years ago
parent 931cb4d719
commit 0c9cf6722d

@ -572,9 +572,9 @@
// test if active_users is valid // test if active_users is valid
$active_users = $content["active_users"]; $active_users = $content["active_users"];
if($active_users == -1) { /*if($active_users == -1) {
$active_users = "N/A"; $active_users = "N/A"; // this breaks sortTable()
} }*/
$line = $line =
" <tr>" . PHP_EOL . " <tr>" . PHP_EOL .

@ -9,30 +9,40 @@ function sortTable(n) {
// Set the sorting direction to ascending: // Set the sorting direction to ascending:
dir = "asc"; dir = "asc";
/* Make a loop that will continue until /* Make a loop that will continue until
no switching has been done: */ no switching has been don*e: */
while (switching) { while (switching) {
// Start by saying: no switching is done: // Start by saying: no switching is done:
switching = false; switching = false;
rows = table.rows; rows = table.rows;
/* Loop through all table rows (except the // Loop through all table rows (except the first, which contains table headers):
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) { for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching: // Start by saying there should be no switching:
shouldSwitch = false; shouldSwitch = false;
/* Get the two elements you want to compare, // Get the two elements you want to compare, one from current row and one from the next:
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD")[n]; x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n]; y = rows[i + 1].getElementsByTagName("TD")[n];
/* Check if the two rows should switch place, // Check if the two rows should switch place, based on the direction, asc or desc:
based on the direction, asc or desc: */
if (dir == "asc") { if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { // If columns is users (3), sort numerically
if ( n == 3 ) {
if (Number(x.innerHTML) > Number(y.innerHTML)) {
shouldSwitch = true;
break;
}
} else if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
// If so, mark as a switch and break the loop: // If so, mark as a switch and break the loop:
shouldSwitch = true; shouldSwitch = true;
break; break;
} }
} else if (dir == "desc") { }
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { else if (dir == "desc") {
if ( n == 3 ) {
// If columns is users (3), sort numerically
if (Number(x.innerHTML) < Number(y.innerHTML)) {
shouldSwitch = true;
break;
}
} else if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
// If so, mark as a switch and break the loop: // If so, mark as a switch and break the loop:
shouldSwitch = true; shouldSwitch = true;
break; break;
@ -40,15 +50,13 @@ function sortTable(n) {
} }
} }
if (shouldSwitch) { if (shouldSwitch) {
/* If a switch has been marked, make the switch // If a switch has been marked, make the switch and mark that a switch has been done:
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true; switching = true;
// Each time a switch is done, increase this count by 1: // Each time a switch is done, increase this count by 1:
switchcount ++; switchcount ++;
} else { } else {
/* If no switching has been done AND the direction is "asc", // If no switching has been done AND the direction is "asc", set the direction to "desc" and run the while loop again.
set the direction to "desc" and run the while loop again. */
if (switchcount == 0 && dir == "asc") { if (switchcount == 0 && dir == "asc") {
dir = "desc"; dir = "desc";
switching = true; switching = true;

Loading…
Cancel
Save