how to display database with javascript in the form of a table?

21,416

You must use the tags (table row) around your rows. Something like this should work:

$.each(query.content,function(data){
        html += "<tr>";
        html += "<td>"+this.no+"</td>";
        html += "<td>"+this.nama+"</td>";
        html += "<td>"+this.alamat+"</td>";
        html += "</tr>";
    });
Share:
21,416
Haviedz Miftah
Author by

Haviedz Miftah

Updated on July 25, 2020

Comments

  • Haviedz Miftah
    Haviedz Miftah almost 4 years

    I have problems with my application that is when it will display the data from the database in the form of a data table that happen to two sideways instead of down

    main.js

    $(document).ready(function(){
    var sql = ign.sql();
    var driver = sql.driver("sqlite","peserta.sqlite");
    var qry = sql.query("select * from data");
    
    if(driver){
        //$(".json").html(JSON.stringify(qry));
        var query = qry;
        var html = "";
    
        $("p").html("Status Database Connection : "+query.status);
        if(query.status){
    
             $.each(query.content,function(data){
                html += "<td>"+this.no+"</td>";
                html += "<td>"+this.nama+"</td>";
                html += "<td>"+this.alamat+"</td>";
            });
        }
    
        $(".data").html(html);
    }
    

    });

    this HTML

    <!doctype html>
    

    <p></p>
    <!--
    <h2>JSON Data</h2>
    <div class="json"></div><hr> -->
    <table>
    <tr>
    <td>No</td>
    <td>Nama</td>
    <td>Alamat</td>
    </tr>
    <tr class="data">
    </tr>
    </table>
    

  • Haviedz Miftah
    Haviedz Miftah over 9 years
    thanks for the answer , but that's not what I mean , should the name of the column underneath the line no , row underneath the name of the column nama and so on .
  • thorsten müller
    thorsten müller over 9 years
    @HaviedzMiftah, ah ok, I understand. That's a bit more complicated, since html doesn't directly allow for "horizontal tables". But you could do it by using this structure, though then you have to use several loops, one for numbers, one for name and one for alamat to generate each row.
  • Haviedz Miftah
    Haviedz Miftah over 9 years
    but I do not understand the existing code between the script tags