const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent; const comparer = (idx, asc) => (a, b) => ((v1, v2) => v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2) )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx)); // Sort work document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => { const table = th.closest('table'); Array.from(table.querySelectorAll('tr:nth-child(n+2)')) .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc)) .forEach(tr => table.appendChild(tr) ); }))); // Search function recherche(select) { // Declare variables var input, filter, table, tr, td, i, txtValue; input = document.getElementById(select); filter = input.value.toUpperCase(); table = document.getElementById("soldes"); tr = table.getElementsByTagName("tr"); th = table.getElementsByTagName("th"); // Loop through all table rows, and hide those who don't match the search query for (i = 0; i < tr.length; i++) { if (select == "pseudo") { td = tr[i].getElementsByTagName("td")[2]; } else if (select == "key") { td = tr[i].getElementsByTagName("td")[1]; } if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } }