processResults.js 1.8 KB

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