results.html 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. })));
  48. </script>
  49. </body>
  50. </html>