【问题标题】:Create table with JSON using Javascript使用 Javascript 使用 JSON 创建表
【发布时间】:2017-05-04 21:16:07
【问题描述】:

我想使用 Javascript 创建带有 JSON 的表。

1。我创建 json API url (http://sample.com/getAllItems)

它会像这样返回 json 数据;

[{"ID":1,"NAME":"Tester","DEPARTMENT":"Dev","TITLE":"sample title","CONTENTS":"sample content","TYPE":0,"IMPORTANCE":"0","CREATED_AT":"2017-04-26 14:55:39","UPDATED_AT":"2017-04-27 00:00:00"},{"ID":4,"NAME":"","DEPARTMENT":"","TITLE":"smaple title2","CONTENTS":"sample content2","TYPE":null,"CREATED_AT":"2017-05-01 11:44:44","UPDATED_AT":"0000-00-00 00:00:00"}]

2。我想用json数据制作表格所以..

    var apiURL = '/getAllItems'; 
    var data= [];  
    var option = [
        {field:"ID",width:10},
        {field:"TITLE", width:160},
        {field:"CONTENTS", width:420}
    ];

    $.getJSON(apiURL, function ( datas ) {
        $.each( datas, function( key, val ){
            data.push( val );
        });
    });

    window.onload = function() {
        var itemTable = $("#itemTable"); 
        var makeTable = $("<table>").appendTo(itemTable); 
        makeTable.css({"border-collapse": "collapse", "border": "1px #CCC solid"});

        $.each( data, function( index, row) {
            var makeTr = $("<tr>").appendTo(makeTable);
            console.log("index : "+index);
            console.log("row : "+ row);

            $.each( option, function( i, fieldInfo ) {
                var makeTd = $("<td>").appendTo(makeTr);
                console.log("Index : "+index);
                console.log("Row : "+row);
                console.log( "i : "+i);
                console.log( "fieldInfo : "+fieldInfo);
                console.log( "fieldInfo.field : "+fieldInfo.field);
                console.log( "Row[Field] : "+row[fieldInfo.field]);

                makeTd.html( row[fieldInfo.field]);
                makeTd.css({"width": fieldInfo.width+"px", "border": "1px #CCC solid"});
            });
        });
    }

</script>

和

返回

--------------------------------------
1   | sample title | sample content
--------------------------------------
2   | sample title2| sample content2
----------------------------------------

我想使用标题单元格,但我不能.. 我怎样才能得到这样的表格?

--------------------------------------
ID  | Title        | Content
--------------------------------------
1   | sample title | sample content
--------------------------------------
2   | sample title2| sample content2
----------------------------------------

【问题讨论】:

    标签: javascript json html-table


    【解决方案1】:

    这是你可以尝试的东西..希望解释有意义..如果有什么你不明白的请继续评论...:D

    var apiURL = '/getAllItems'; 
        var data= [];  
        var option = [
            {field:"ID",width:10},
            {field:"TITLE", width:160},
            {field:"CONTENTS", width:420}
        ];
    
    //commented your Ajax for testing.
    
        /*$.getJSON(apiURL, function ( datas ) {
            $.each( datas, function( key, val ){
                data.push( val );
            });
        });
    */
    
    //get the data varable set
    
    data = [{"ID":1,"NAME":"Tester","DEPARTMENT":"Dev","TITLE":"sample title","CONTENTS":"sample content","TYPE":0,"IMPORTANCE":"0","CREATED_AT":"2017-04-26 14:55:39","UPDATED_AT":"2017-04-27 00:00:00"},{"ID":2,"NAME":"","DEPARTMENT":"","TITLE":"smaple title2","CONTENTS":"sample content2","TYPE":null,"CREATED_AT":"2017-05-01 11:44:44","UPDATED_AT":"0000-00-00 00:00:00"}];
    
    
    //begin creating table on load.
    window.onload = function() {
            
            //create table element.
            var makeTable = $("<table>");
            //append table to existing div here it have class "test".
            $(".test").append($(makeTable));
            //append first row for headings.
            $(makeTable).append($("<tr>"));
            
            //append all heading in loop.
            $.each(option,function(i,dt){
                //simply find first tr and append td with content to it.
                $(makeTable).find("tr").append("<td>"+dt["field"]+"</td>");
            });
        
          // start appending data.
          $.each(data,function(i,row){
            //find tbody in table and add tr to it.
               var nrow = $(makeTable).find('tbody').append("<tr>"); 
           //for each options find its value in data and append to newly created tr above.
               $.each(option,function(j,dt){
                  // this is actually what you want..  
                  $(nrow).append("<td>"+row[dt["field"]]+"</td>");
                });
           });
        }
    
        //done.
    table,td
    {
    border: 1px #CCC solid;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
    <div class="test">
    </div>

    【讨论】:

    • downvoters..请考虑添加原因或 cmets..只是按喇叭按钮不会增加您的价值...
    • 哇,它有效!谢谢你的好意!这是非常有用的例子。 .. 我投票这个答案很有用,但它说“感谢您的反馈!声望低于 15 的人的投票将被记录,但不要更改公开显示的帖子分数。” ..我很抱歉有人投票没有用,但你的回答对我来说是最好的解决方案。谢谢。
    • 没关系..我想如果它解决了您的问题,您可以将其标记为答案休息很乐意提供帮助...我们在这里提供帮助、分享和成长...干杯
    【解决方案2】:
    var makeTh = $("<th>").appendTo(makeTable);
    $.each(option, function (index, row) {
        var makeTd = $("<td>").append(makeTh);
        makeTd.css({width, row['width']}).html(row['field']);
    });
    

    尝试在第一个 $.each 上添加代码。

    【讨论】:

    • 我认为这是合乎逻辑的解决方案,但它不起作用..我不明白为什么(T_T)
    • 只需循环 option 变量以呈现 th 标记。
    【解决方案3】:

    您必须在循环开始之前添加标题行

     var apiURL = '/getAllItems'; 
        var data= [];  
        var option = [
            {field:"ID",width:10},
            {field:"TITLE", width:160},
            {field:"CONTENTS", width:420}
        ];
    
        $.getJSON(apiURL, function ( datas ) {
            $.each( datas, function( key, val ){
                data.push( val );
            });
        });
    
        window.onload = function() {
            var itemTable = $("#itemTable"); 
            var makeTable = $("<table>").appendTo(itemTable); 
            var head='<tr><th>ID</th><th>TITLE</th><th>CONTENT</th></tr>';
            makeTable.css({"border-collapse": "collapse", "border": "1px #CCC solid"});
              $(head).appendTo(makeTable);
            $.each( data, function( index, row) {
                var makeTr = $("<tr>").appendTo(makeTable);
                console.log("index : "+index);
                console.log("row : "+ row);
    
                $.each( option, function( i, fieldInfo ) {
                    var makeTd = $("<td>").appendTo(makeTr);
                    console.log("Index : "+index);
                    console.log("Row : "+row);
                    console.log( "i : "+i);
                    console.log( "fieldInfo : "+fieldInfo);
                    console.log( "fieldInfo.field : "+fieldInfo.field);
                    console.log( "Row[Field] : "+row[fieldInfo.field]);
    
                    makeTd.html( row[fieldInfo.field]);
                    makeTd.css({"width": fieldInfo.width+"px", "border": "1px #CCC solid"});
                });
            });
        }
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 2013-01-16
      相关资源
      最近更新 更多