results.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. <script>
  15. let filterTable = function(value) {
  16. const filter = value.toUpperCase();
  17. let table = document.getElementsByTagName("tbody")[0];
  18. let tr = table.getElementsByTagName("tr");
  19. for (let i = 0; i < tr.length; i++) {
  20. tr[i].style.display = "none";
  21. let td = tr[i].getElementsByTagName("td");
  22. for (var j = 0; j < td.length; j++) {
  23. let cell = tr[i].getElementsByTagName("td")[j];
  24. if (cell) {
  25. if (cell.innerText.toUpperCase().indexOf(filter) > -1) {
  26. tr[i].style.display = "";
  27. break;
  28. }
  29. }
  30. }
  31. }
  32. };
  33. let clearFilter = function() {
  34. let table = document.getElementsByTagName("tbody")[0];
  35. let tr = table.getElementsByTagName("tr");
  36. for (let i = 0; i < tr.length; i++) {
  37. tr[i].style.display = "";
  38. }
  39. };
  40. </script>
  41. </head>
  42. <body>
  43. <a href="/">Search again</a>
  44. <div>Filter:
  45. <input type="text" name="name" onchange="filterTable(this.value)" autocomplete="off">
  46. <button class="btn" onclick="clearFilter()">Clear</button>
  47. </div>
  48. <table>
  49. <thead>
  50. <tr style='text-align: left'>
  51. <th>Source</th>
  52. <th>Translation</th>
  53. <th>TM</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. {{range $result := .Results}}
  58. {{range $segment := $result.Segments}}
  59. <tr style='text-align: left'>
  60. <td>{{$segment.Source}}</td>
  61. <td>{{$segment.Target}}</td>
  62. <td>{{$result.TMName}}</td>
  63. </tr>
  64. {{end}}
  65. {{end}}
  66. </tbody>
  67. </table>
  68. <br/>
  69. <a href="/">Search again</a>
  70. <script>
  71. // borrowed from https://stackoverflow.com/a/49041392/7342859
  72. const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
  73. const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
  74. v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
  75. )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
  76. // do the work...
  77. document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
  78. const table = th.closest('table');
  79. Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
  80. .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
  81. .forEach(tr => table.appendChild(tr) );
  82. const sorted = " (sorted)";
  83. if (!th.textContent.includes(sorted)) {
  84. th.textContent += sorted;
  85. }
  86. th.parentNode.childNodes.forEach((child) => {
  87. if (child != th) {
  88. child.textContent = child.textContent.replace(sorted, "");
  89. }
  90. });
  91. })));
  92. </script>
  93. </body>
  94. </html>