56 lines
No EOL
1.9 KiB
JavaScript
56 lines
No EOL
1.9 KiB
JavaScript
let sortDirection = {};
|
|
|
|
function sortTable(tableId, col) {
|
|
const table = document.getElementById(tableId);
|
|
if (!table) return;
|
|
|
|
// Initialize this table's sort direction if not set
|
|
if (!sortDirection[tableId]) {
|
|
sortDirection[tableId] = {};
|
|
}
|
|
|
|
const dir = sortDirection[tableId][col] = !sortDirection[tableId][col];
|
|
const rows = Array.from(table.tBodies[0].rows);
|
|
|
|
rows.sort((a, b) => {
|
|
const aCell = a.cells[col];
|
|
const bCell = b.cells[col];
|
|
|
|
if (col === 1) { // If sorting by visit count (numeric column)
|
|
const aValue = parseInt(aCell.innerText.trim()) || 0;
|
|
const bValue = parseInt(bCell.innerText.trim()) || 0;
|
|
return dir ? bValue - aValue : aValue - bValue;
|
|
} else { // Sorting by text (browser/os/other columns)
|
|
const aText = aCell.innerText.trim();
|
|
const bText = bCell.innerText.trim();
|
|
return dir ? bText.localeCompare(aText) : aText.localeCompare(bText);
|
|
}
|
|
});
|
|
|
|
// Re-attach the sorted rows
|
|
rows.forEach(row => table.tBodies[0].appendChild(row));
|
|
|
|
// Update sorting indicators
|
|
Array.from(table.querySelectorAll('th')).forEach((th, idx) => {
|
|
th.classList.remove('sorted-asc', 'sorted-desc');
|
|
if (idx === col) {
|
|
th.classList.add(dir ? 'sorted-desc' : 'sorted-asc');
|
|
}
|
|
});
|
|
}
|
|
|
|
// Initialize table sorting when the document is loaded
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Apply sorting to tables with sortable class
|
|
const tables = document.querySelectorAll('.jobsTable');
|
|
tables.forEach(table => {
|
|
const tableId = table.id;
|
|
const headers = table.querySelectorAll('th');
|
|
|
|
headers.forEach((header, idx) => {
|
|
header.addEventListener('click', function() {
|
|
sortTable(tableId, idx);
|
|
});
|
|
});
|
|
});
|
|
}); |