javascript 如何以表格的形式使用javascript显示数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26946099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:44:16  来源:igfitidea点击:

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

javascripthtmlsqlite

提问by Haviedz Miftah

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

这个 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>

采纳答案by thorsten müller

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>";
    });

回答by Akshar

In your html, replace

在你的 html 中,替换

<tr class="data">

with

<tbody class="data">

and in your jQuery, do what @thorsten has suggested.

在您的 jQuery 中,按照 @thorsten 的建议进行操作。

Also, you might have to replace the <td>tags in your static html to <th>, to make them headings and force browsers to render it as .

此外,您可能需要将<td>静态 html 中的标签替换为<th>,以使它们成为标题并强制浏览器将其呈现为 .