Firebase - Print records in descending order

Asked

Viewed 475 times

1

I can’t print the records in descending order. I looked on several websites for tips that could help me, but when I adapt to my code, nothing works!

My question is this::

I’m creating a game on Intel XDK using Firebase as a database and Javascript to handle it, but I can’t display the logs on rising order and neither decreasing, which is what I want. Equal to a Ranking.

I have the following code:

<html>
<head>
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale= 1.0, maximum-scale= 1.0, user-scalable=no />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <script src="https://cdn.firebase.com/js/client/1.0.17/firebase.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.css></script>
</head>

<body>
<table>
    <thead>
        <th>NOME</th>
        <th>TEMPO</th>
        <th>ACERTOS</th>
        <th>ERROS</th>
    </thead>
    <tbody id="resultado">

    </tbody>
</table>
<script>
    $(document).ready(function(){
        var APP = new Firebase("https://testestroop-c52df.firebaseio.com/");
        APP.ref().child('jogada').on('child_added', function(snap){
            var jogada = snap.val();
            var tr = document.createElement('tr');
            var td1 = document.createElement('td');
            var td2 = document.createElement('td');
            var td3 = document.createElement('td');
            var td4 = document.createElement('td');

            td1.appendChild(document.createTextNode(jogada.nomeJogador));
            tr.appendChild(td1);
            td2.appendChild(document.createTextNode(jogada.tempo));
            tr.appendChild(td2);
            td3.appendChild(document.createTextNome(jogada.acertos));
            tr.appendChild(td3);
            td4.appendChild(document.createTextNode(jogada.erros));
            tr.appendChild(td4);
            resultado.appendChild(tr);
        }
    });
</script>
</body>
</html>

And in this image below is the structure of the database in Firebase:

Essa é a estrutura da base de dados no Firebase

I really appreciate your help.

1 answer

1


The problem is that there were "loose ends" in the code such as the lack of some arguments ) in jQuery code and you also had a typo that was on the line td3.appendChild(document.createTextNome(jogada.acertos)); instead of createTextNode.

In the <head> jQuery library was also not being called correctly and missing closure of a ", she was with the extension .css instead of .js :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.css></script>

Here’s a working online example - https://jsfiddle.net/shuffledPixels/tcwpg73j/

And here’s the code your complete code:

<html>
<head>
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale= 1.0, maximum-scale= 1.0, user-scalable=no />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <!-- <meta charset="UTF-8"> Utiliza isto em vez da linha acima se estás a construir esta plataforma como um projeto recente -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="https://cdn.firebase.com/js/client/1.0.17/firebase.js"></script>
</head>

<body>
<table>
    <thead>
        <th>NOME</th>
        <th>TEMPO</th>
        <th>ACERTOS</th>
        <th>ERROS</th>
    </thead>
    <tbody id="resultado">

    </tbody>
</table>

<script>
$(document).ready(function(){
    var APP = new Firebase("https://testestroop-c52df.firebaseio.com/");
    APP.ref().child('jogada').on('child_added', function(snap){
        var jogada = snap.val();
        var tr = document.createElement('tr');
        var td1 = document.createElement('td');
        var td2 = document.createElement('td');
        var td3 = document.createElement('td');
        var td4 = document.createElement('td');

        td1.append(document.createTextNode(jogada.nomeJogador));
        tr.appendChild(td1);
        td2.append(document.createTextNode(jogada.tempo));
        tr.appendChild(td2);
        td3.append(document.createTextNode(jogada.acertos));
        tr.appendChild(td3);
        td4.append(document.createTextNode(jogada.erros));
        tr.appendChild(td4);
        resultado.appendChild(tr);
    });
});
</script>
<body>
</html>
  • Hi Chun, my computer was crashing a lot so I had to type all the code through the phone, I just saw that I forgot to put commas, semicolons, those things. But the code is working perfectly. My question is as follows: How to display the Firebase records for hits in descending order. As a ranking, whoever has the most hits appears at the top of the table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.