JavaScript - SortTable

 
Vista:
sin imagen de perfil

SortTable

Publicado por Cesar (1 intervención) el 02/05/2022 02:44:59
Captura-de-pantalla-de-2022-05-01-20-39-12

Tengo el problema de que el no ordena bien la columa de porcentajes. Por alguna razón el 59 queda antes del 60 y no he podido entender porqué pasa. Les dejo el código.

function sortTable1() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("myTable");
switching = true;
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[1];
y = rows[i + 1].getElementsByTagName("TD")[1];
//check if the two rows should switch place:
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

SortTable

Publicado por antonio (16 intervenciones) el 02/05/2022 15:52:48
Pues no lo se debería funcionar bien yo he buscado y he encontrado esta pagina donde explica todo sorttable en ingles:
https://www.kryogenix.org/code/browser/sorttable/

Este es el código ejemplo que he hecho y funciona correctamente


<!DOCTYPE html>
<html lang="es">
<head>
<script src="sorttable.js"></script>
</head>
<body class="container-fluid p-5">

<table class="sortable">
<thead>
<tr>
<th>Sucursal</th>
<th>Puntuaje </th>
<th>Asesor Técnico </th>
</tr>
</thead>
<tbody>
<tr>
<td>Noack la reina</td>
<td>59%</td>
<td>Daniela Calvo Salinas</td>

</tr>
<tr>
<td>Siglo XXI los andes</td>
<td>60%</td>
<td>Benjamin Antonio Duran Rivas</td>

</tr>
<tr>
<td>Aventura motors vitacura</td>
<td>58%</td>
<td>Cintia Amanda diaz Cuba</td>

</tr>
<tr>
<td>Aventura motors vitacura</td>
<td>55%</td>
<td>Carlos Francisco Barahona Herrera</td>

</tr>
<tr>
<td>Rosselot Talca</td>
<td>54%</td>
<td>German Esteban Gozález Aldana</td>

</tr>

</tbody>
</table>




<style>
table.sortable thead {
background-color:#eee;
color:#666666;
font-weight: bold;
cursor: default;
}
table.sortable th:not(.sorttable_sorted):not(.sorttable_sorted_reverse):not(.sorttable_nosort):after {
content: " \25B4\25BE"
}
</style>

</body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar