在JavaScript中从JSON对象创建表 [英] Create table from JSON object in JavaScript

查看:149
本文介绍了在JavaScript中从JSON对象创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个HTML表格,其中包含我用来绘制信息的HTML表格。
我不想查询数据库两次,我想创建图表和一个表的信息。
这是我从服务器得到的,并且它得到了图表:

I am trying to creat an HTML table with information I use to draw at flot. I dont want to query the database twice, and I want to creat the chart and a table with the information. This is what I get from the server and that it gets charted:

var data = {
              "selector":"#charttotalday",
              "jsondata":[
                 {
                    "label":"Client1",
                    "data":[
                            [1382670000000,110157],
                            [1382756400000,199055],
                            [1382842800000,362996],
                            [1382929200000,302],
                            [1383015600000,169],
                            [1383102000000,88],
                            [1383188400000,49]
                            ],
                    "points":{
                       "fillColor":"#88bbc8"
                    }
                 },
                 {
                    "label":"Client2",
                    "data":[
                            [1382670000000,58611],
                            [1382756400000,112268],
                            [1382842800000,193060],
                            [1382929200000,115],
                            [1383015600000,45],
                            [1383102000000,65],
                            [1383188400000,18]
                            ],
                    "points":{
                       "fillColor":"#ed7a53"
                    }
                 },
                 {
                    "label":"Client3",
                    "data":[
                            [1382670000000,65534],
                            [1382756400000,118362],
                            [1382842800000,200058],
                            [1382929200000,123],
                            [1383015600000,65],
                            [1383102000000,53],
                            [1383188400000,26]
                    ],
                    "points":{
                       "fillColor":"#9FC569"
                    }
                 }
              ]
           };

由于组织信息的方式,我不能使用$ .each创建以下格式的表:

Because of the way the information is organized, I can not use $.each to loop over it and create a table of the format:

                Client1  Client2  Client3
1382670000000 |  10157 |  58611 |  65534 | 
1382756400000 |  99055 |  12268 |  18362 |
1382842800000 |  62996 |  93060 |  10058 |
1382929200000 |    302 |    115 |    123 |
1383015600000 |    169 |     45 |     65 |
1383102000000 |     88 |     65 |     53 |
1383188400000 |     49 |     18 |     26 |

我认为最好的方法是读取对象并创建一个新的结构

I am thinking that the best way would be to read the object and create a new one with the structure that I need, that can be used with $.each.

我试过这个:

$.each(data.jsondata, function(key, value) {
    thead += '<th>' + value.label + '</th>';
    tableData[value.label] = new Object();
    $.each(value.data, function(key1, value1) {
        $.each(value1, function(key2, value2) {
            if(key2 == 0) {
                //here I have the time line, axis Y
                index = value2;
            }
            if(key2 == 1) {
                //here I have the values for the table
                tableData[value.label][index] = value2;
            }
        });
    });
});

thead += '</tr>';

但是这只会创建一个更简单的元素与我需要的信息,我需要。

But this only creats a more simple element with the information that I need, but still cant turn into what I need.

有人可以指点我的方向吗?
谢谢!

Can someone please point me in the right direction? Thanks!

推荐答案

这将将您的数据映射到表:

This will map your data to table:

 var headers=[ ""], rows={}
  $.each(data.jsondata,function(clientIdx,item){
      headers.push(item.label );
      $.each( item.data,function(i,arr){
        if( !rows[arr[0]]){
         rows[arr[0]]=[];
        }
        rows[arr[0]][clientIdx]=arr[1];
      })
  })


  var rowHtml='<tr><th>'+headers.join('</th><th>')+'</th></tr>';
  $.each(rows,function(key, arr){
    rowHtml+='<tr><td>'+key+'</td>';
    rowHtml +='<td>'+arr.join('</td><td>')+'</td></tr>';

  })

  $('#table').html(rowHtml);

DEMO

这篇关于在JavaScript中从JSON对象创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆