results.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. let filter = document.getElementById("filter");
  40. filter.value = "";
  41. };
  42. </script>
  43. </head>
  44. <body>
  45. <a href="/">Search again</a>
  46. <div>Filter:
  47. <input id="filter" type="text" name="name" onchange="filterTable(this.value)" autocomplete="off">
  48. <button class="btn" onclick="clearFilter()">Clear</button>
  49. </div>
  50. <table>
  51. <thead>
  52. <tr style='text-align: left'>
  53. <th>Source</th>
  54. <th>Translation</th>
  55. <th>TM</th>
  56. </tr>
  57. </thead>
  58. <tbody>
  59. {{range $result := .Results}}
  60. {{range $segment := $result.Segments}}
  61. <tr style='text-align: left'>
  62. <td>{{$segment.Source}}</td>
  63. <td>{{$segment.Target}}</td>
  64. <td>{{$result.TMName}}</td>
  65. </tr>
  66. {{end}}
  67. {{end}}
  68. </tbody>
  69. </table>
  70. <br/>
  71. <a href="/">Search again</a>
  72. <script>
  73. // borrowed from https://stackoverflow.com/a/49041392/7342859
  74. const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
  75. const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
  76. v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
  77. )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
  78. // do the work...
  79. document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
  80. const table = th.closest('table');
  81. Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
  82. .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
  83. .forEach(tr => table.appendChild(tr) );
  84. const sorted = " (sorted)";
  85. if (!th.textContent.includes(sorted)) {
  86. th.textContent += sorted;
  87. }
  88. th.parentNode.childNodes.forEach((child) => {
  89. if (child != th) {
  90. child.textContent = child.textContent.replace(sorted, "");
  91. }
  92. });
  93. })));
  94. </script>
  95. </body>
  96. </html>