繁体   English   中英

用JSON数据填充动态表

[英]Populating dynamic table with json data

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  <title>MongoDB Display</title>
<script>
$(document).ready(function(){
    var Obj = $.getJSON("http://localhost:3000", function(){
        alert("Success");

        $(Obj.rows).each(function(index, element){
            $(#tableID).append('<tr><td>' + element[1] + '</td><td><table><tr><td>'+ Obj.rows.scripts.element[0] 
            + '</td>' + "/" + '<td>' + Obj.rows.scripts.element[1] + '</td></tr></table></td></tr>');
        })
    });
});
</script>

</head>
<body>
    <div id="wrapper">
        <h1>H 1</h1>
        <table id="tableID" border = "1">
            <th>
                <td>PAGE</td>
                <td>SCRIPTS</td>
            </th>
        </table>
    </div>
</body>
</html>

简而言之;

我有一个MongoDB准备在localhost:3000上显示。 服务器正在运行,我可以使用localhost:3000在浏览器上显示数据。但是,当我尝试在浏览器上运行以上代码时,我都能得到js Alert("Success")和表的硬编码部分。 页面和脚本...

数据是:

{
    "offset": 0,
        "rows": [
            {
                "_id": {},
                "url": "http://www.qweq.com\r",
                "totalLines": 3084,
                "scripts": [
                    {
                        "line": 111,
                        "tag": "\"asdas/etc/designs/qwe/assets/head-5.30.31.min.js\""
                    },{
                        "line": 3065,
                        "tag": "\"asdas/etc/designs/qwe/assets/common-5.30.31.min.js\""
                    },{
                        "line": 3067,
                        "tag": "\"asda/etc/designs/qwe/assets/category-5.30.31.min.js\""
                    }
                ]
            },{
                "_id": {},
                "url": "http://www.qweqsd.com/qwdq23/qweq/qweqw/\r",
                "totalLines": 3042,
                "scripts": [
                    {
                        "line": 113,
                        "tag": "\"asda/etc/designs/asd/assets/head-5.30.31.min.js\""
                    },{
                        "line": 3023,
                        "tag": "\"asdasd/etc/designs/asd/assets/common-5.30.31.min.js\""
                    },{
                        "line": 3025,
                        "tag": "\"asdad/etc/designs/qwe/assets/category-5.30.31.min.js\""
                    }
                ]
            },

如何使此Json数据显示在动态表中?

谢谢。

这个版本是可行的:)也许您会找到灵感。

$(rows).each(function(index,element){
    console.log(index); console.log(element);
    $('#tableID').append('<tr><td>' + index + '</td><td>'+ element['url'] 
                         + '</td>' + "/" + '<td>' + JSON.stringify(element['scripts']) + '</td></tr></table>');
});

http://jsfiddle.net/fw4f5L72/1/

好像您在表名周围缺少引号。 相反,您应该具有类似$('#tableID')

此外,您将希望函数调用包含返回给您的数据。 例如:

$.getJSON("http://localhost:3000", function(data){
        alert("Success");
        console.log(data);
    });

然后, data将包含JSON对象,您可以随意对其进行操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM