dimanche 18 juillet 2021

Sort a list on a Database wiht javasctipt

I enter data (name and score) and I would like them to be ordered by score when entering the list.

I want the data to appear ordered at the bottom I enter the data but they remain according to the order they were placed

tanks you

link to the code:

function capturar(){
   function Persona(nombre, puntos){
       this.nombre = nombre;
       this.puntos = puntos;
   }
   var nombreCapturado = document.getElementById("nombre").value;
   var puntosCapturados = document.getElementById("puntos").value;
    nuevoPersona = new Persona (nombreCapturado, puntosCapturados);
   console.log(nuevoPersona);
   baseDatos.sort((a,b) => {
    if(a.puntos < b.puntos){
        return -1;
    }
    if (a.puntos > b.puntos){
        return 1;
    } 

    return 0;
});
[enter link description here][1]
   guardarData();


}

var baseDatos=[];
function guardarData(){
    baseDatos.push(nuevoPersona);
    
    document.getElementById("tabla").innerHTML+= '<td class="td">'+nuevoPersona.nombre+' </td><td class="td">'+nuevoPersona.puntos+'</td>';
    console.log("la base de datos con : "+nuevoPersona.nombre + " puntos : " +nuevoPersona.puntos);
};

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/styles.css">
    <script src="js/puntaje.js"></script>
    <title>WebApp de Mejor Puntaje</title>
</head>
<body>
    
    <main>
        <h1 class="TituloH1">Ingresa el puntaje para ver en que posiscion quedaste</h1>
        <div id="DataInput">
            <Input class="InpPunt" placeholder="Igresa tu nombre" id="nombre" required></Input>
            <Input class="InpPunt" placeholder="Igresa tus Puntos" type="number" id="puntos" required></Input>
            <button class="BtnData" onclick="capturar()">SAVE</button>
        </div>

        <div class="puntajesAll">
            <table class="table" id="tabla">
                <thead class="thead">
                    <tr>
                        <th class="thName">Puntos </th>
                        <th class="thName">Nombre</th>
                    </tr>
                </thead>
                <tbody class="tbody">
                    
                    

                </tbody>
            </table>

        </div>
    </main>
</body>

</html>



Aucun commentaire:

Enregistrer un commentaire