processResults.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. let app = {};
  2. app.filterTable = function(value) {
  3. const filter = value.toUpperCase();
  4. let table = document.getElementsByTagName("tbody")[0];
  5. let tr = table.getElementsByTagName("tr");
  6. for (let i = 0; i < tr.length; i++) {
  7. tr[i].style.display = "none";
  8. let td = tr[i].getElementsByTagName("td");
  9. for (var j = 0; j < td.length; j++) {
  10. let cell = tr[i].getElementsByTagName("td")[j];
  11. if (cell) {
  12. if (cell.innerText.toUpperCase().indexOf(filter) > -1) {
  13. tr[i].style.display = "";
  14. break;
  15. }
  16. }
  17. }
  18. }
  19. };
  20. app.clearFilter = function() {
  21. let table = document.getElementsByTagName("tbody")[0];
  22. let tr = table.getElementsByTagName("tr");
  23. for (let i = 0; i < tr.length; i++) {
  24. tr[i].style.display = "";
  25. }
  26. let filter = document.getElementById("filter");
  27. filter.value = "";
  28. };
  29. app.sortTable = function() {
  30. // borrowed from https://stackoverflow.com/a/49041392/7342859
  31. const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
  32. const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
  33. v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
  34. )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
  35. // do the work...
  36. document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
  37. const table = th.closest('table');
  38. Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
  39. .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
  40. .forEach(tr => table.appendChild(tr) );
  41. const sorted = " (sorted)";
  42. if (!th.textContent.includes(sorted)) {
  43. th.textContent += sorted;
  44. }
  45. th.parentNode.childNodes.forEach((child) => {
  46. if (child != th) {
  47. child.textContent = child.textContent.replace(sorted, "");
  48. }
  49. });
  50. })));
  51. };