results.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Search results for "{{.SearchPhrase}}"</title>
  5. <style>
  6. table, th, td {
  7. border: 1px solid black;
  8. border-collapse: collapse;
  9. }
  10. th {
  11. cursor: pointer;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <a href="/">Search again</a>
  17. <table>
  18. <tr style='text-align: left'>
  19. <th>Source</th>
  20. <th>Translation</th>
  21. <th>TM</th>
  22. </tr>
  23. {{range $result := .Results}}
  24. {{range $segment := $result.Segments}}
  25. <tr style='text-align: left'>
  26. <td>{{$segment.Source}}</td>
  27. <td>{{$segment.Target}}</td>
  28. <td>{{$result.TMName}}</td>
  29. </tr>
  30. {{end}}
  31. {{end}}
  32. </table>
  33. <br/>
  34. <a href="/">Search again</a>
  35. <script>
  36. // borrowed from https://stackoverflow.com/a/49041392/7342859
  37. const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
  38. const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
  39. v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
  40. )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
  41. // do the work...
  42. document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
  43. const table = th.closest('table');
  44. Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
  45. .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
  46. .forEach(tr => table.appendChild(tr) );
  47. const sorted = " (sorted)";
  48. if (!th.textContent.includes(sorted)) {
  49. th.textContent += sorted;
  50. }
  51. th.parentNode.childNodes.forEach((child) => {
  52. if (child != th) {
  53. child.textContent = child.textContent.replace(sorted, "");
  54. }
  55. });
  56. })));
  57. </script>
  58. </body>
  59. </html>