support sorting stats table

This commit is contained in:
Ben Harris 2019-07-29 00:44:28 -04:00
parent c593396370
commit 09bdeef012
1 changed files with 22 additions and 1 deletions

View File

@ -31,6 +31,27 @@ include __DIR__."/../header.php";
</table>
</div>
<hr>
<p>Available in <a href="/stats.json">JSON format</a>.</p>
<p>also available as <a href="/stats.json">json</a>.</p>
<script>
// sort stats page
const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
)(getCellValue(asc ? b : a, idx), getCellValue(asc ? a : b, idx));
// do the work...
document.querySelectorAll('th').forEach(function(th) {
th.addEventListener('click', (() => {
const table = th.closest('table').querySelector("tbody");
Array.from(table.querySelectorAll('tr'))
.sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
.forEach(tr => table.appendChild(tr) );
}));
th.style.cursor = 'pointer';
});
</script>
<?php include __DIR__."/../footer.php"; ?>