从JSON生成javascript中的HTML表格

时间:2016-04-19 13:34:28

标签: javascript jquery html json html-table

我想从这种类型的JSON生成HTML表:

{  
   "fields":{  
      "Product.Name":[  
         "ql23xx-firmware",
         "setup",
         "ql2500-firmware"
      ],
      "Product.Version":[  
         "3.03.27-3.1.el6-noarch",
         "2.8.14-13.el6-noarch",
         "5.06.02-1.el6-noarch"
      ],
      "Id":[  
         "tdcapa11s"
      ],
      "CMDB.EntityInCharge":[  
         "SUP.CA_SYS"
      ]
   }
}

我可以轻松地做一个像这样的表:

<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>CMDB.EntityInCharge</th>
            <th>Product.Name</th>
            <th>Product.Version</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>tdcapa11s</td>
            <td>SUP.CA_SYS</td>
            <td>ql23xx-firmware, setup, ql2500-firmware</td>
            <td>3.03.27-3.1.el6-noarch, 2.8.14-13.el6-noarch, 5.06.02-1.el6-noarch</td>
        </tr>
    </tbody>
</table>

但我想要的是:

<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>CMDB.EntityInCharge</th>
            <th>Product.Name</th>
            <th>Product.Version</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>tdcapa11s</td>
            <td>SUP.CA_SYS</td>
            <td>ql23xx-firmware</td>
            <td>3.03.27-3.1.el6-noarch</td>
        </tr>
        <tr>
            <td>tdcapa11s</td>
            <td>SUP.CA_SYS</td>
            <td>setup</td>
            <td>2.8.14-13.el6-noarch</td>
        </tr>
        <tr>
            <td>tdcapa11s</td>
            <td>SUP.CA_SYS</td>
            <td>ql2500-firmware</td>
            <td>5.06.02-1.el6-noarch</td>
        </tr>
    </tbody>
</table>

如何使用Javascript生成此内容?

我离这不远,但我的javascript代码对于像这样简单的事情实际上很长

1 个答案:

答案 0 :(得分:1)

以下是一种方法:

body_row.append( td.clone().html( (field == 'Id' || field == 'CMDB.EntityInCharge') ? data.fields[field][0] : data.fields[field][i] ) );

如下面的演示:

var data = {  
   "fields":{  
      "Id":[  
         "tdcapa11s"
      ],
      "CMDB.EntityInCharge":[  
         "SUP.CA_SYS"
      ],
      "Product.Name":[  
         "ql23xx-firmware",
         "setup",
         "ql2500-firmware"
      ],
      "Product.Version":[  
         "3.03.27-3.1.el6-noarch",
         "2.8.14-13.el6-noarch",
         "5.06.02-1.el6-noarch"
      ]
   }
}
var table = $('<table/>'),
    thead = $('<thead/>'),
    tbody = $('<tbody/>'),
    tr    = $('<tr/>'),
    th    = $('<th/>'),
    td    = $('<td/>');
//header row
var hrow = tr.clone();
for(field in data.fields) {
    hrow.append( th.clone().html( field ) );
}
//append row to thead and table
table.html( thead.html( hrow ) );
//body rows
for( var i = 0; i < data.fields["Product.Name"].length; i++ ) {
    var brow = tr.clone();
    for(field in data.fields) {
        brow.append( td.clone().html( (field == 'Id' || field == 'CMDB.EntityInCharge') ? data.fields[field][0] : data.fields[field][i] ) );
    }
    //append row to tbody
    tbody.append( brow );
}
//append tbody to table
table.append( tbody );
//append table to page
$('#table').html( table );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="table"></div>