From 9e25f0023c8aa22128ded2f3e4f3528c221eedcb Mon Sep 17 00:00:00 2001 From: James Pattinson Date: Mon, 17 Mar 2025 20:37:04 +0000 Subject: [PATCH] Admin sort --- admin.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/admin.php b/admin.php index 190adb9..1c33797 100644 --- a/admin.php +++ b/admin.php @@ -20,6 +20,51 @@ require_db_auth(); row.classList.add("highlightJET"); // Add the class } }); + + // Sorting functionality + let table = document.querySelector("table"); + let headers = table.querySelectorAll("th"); + let sortDirection = {}; // Track sort direction for each column + + headers.forEach((header, index) => { + header.addEventListener("click", function() { + let direction = sortDirection[index] === "asc" ? "desc" : "asc"; + sortDirection[index] = direction; + sortTable(table, index, direction); + + // Update visual indication + headers.forEach(h => h.classList.remove("sorted-asc", "sorted-desc")); + header.classList.add(direction === "asc" ? "sorted-asc" : "sorted-desc"); + }); + }); + + function sortTable(table, columnIndex, direction) { + let rows = Array.from(table.querySelectorAll("tbody tr")); + let isDate = !isNaN(Date.parse(rows[0].cells[columnIndex]?.textContent.trim() || "")); + let isNumeric = !isNaN(rows[0].cells[columnIndex]?.textContent.trim() || ""); + let sortedRows = rows.sort((a, b) => { + let aText = a.cells[columnIndex]?.textContent.trim() || null; + let bText = b.cells[columnIndex]?.textContent.trim() || null; + + if (aText === null || bText === null) { + return aText === null ? 1 : -1; // Place null/empty values at the bottom + } + + if (isDate) { + let aDate = new Date(aText); + let bDate = new Date(bText); + return direction === "asc" ? aDate - bDate : bDate - aDate; + } else if (isNumeric) { + return direction === "asc" ? aText - bText : bText - aText; + } else { + return direction === "asc" ? aText.localeCompare(bText) : bText.localeCompare(aText); + } + }); + + let tbody = table.querySelector("tbody"); + tbody.innerHTML = ""; + sortedRows.forEach(row => tbody.appendChild(row)); + } }); @@ -91,6 +136,16 @@ require_db_auth(); border-radius: 5px; } + /* Add styles for sorted columns */ + th.sorted-asc::after { + content: " ▲"; + font-size: 0.8em; + } + th.sorted-desc::after { + content: " ▼"; + font-size: 0.8em; + } + @@ -241,7 +296,6 @@ if ($result->num_rows > 0) { echo '' . htmlspecialchars($value ?? '') . ''; } } - echo ''; echo ''; }