首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jquery json 生成动态报表

2013-02-18 
jquery json 生成动态表格一组json数据如下:[{RRYID:039,公共部分:22.0440,设备管理:0,班组

jquery json 生成动态表格
一组json数据如下:


[{"RRYID":"039","公共部分":"22.0440","设备管理":"0","班组管理":"0","运行管理":"-0.20","安全管理":"-0.10"},{"RRYID":"586","公共部分":"33.2670","设备管理":"0","班组管理":"0","运行管理":"-1.50","安全管理":"-0.20"},{"RRYID":"429","公共部分":"10.7290","设备管理":"0","班组管理":"0","运行管理":"-0.20","安全管理":"0"},{"RRYID":"372","公共部分":"54.4370","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"0"},{"RRYID":"061","公共部分":"29.7760","设备管理":"0","班组管理":"0","运行管理":"-0.20","安全管理":"-0.20"},{"RRYID":"008","公共部分":"19.1960","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"-0.50"},{"RRYID":"0363","公共部分":"27.6040","设备管理":"0","班组管理":"0","运行管理":"-1","安全管理":"0"},{"RRYID":"0521","公共部分":"38.0160","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"0"},{"RRYID":"0163","公共部分":"27.7390","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"-0.60"}]


使用Jquery如何将这组以表格形式显示出来。注意:这里的数据是从数据库中读取来的,是动态的,主要是列数不固定,那种列数固定的我会,不固定的不知道怎么写,请指教,谢谢
[解决办法]
引用:
一组json数据如下:
XML/HTML code?1[{"RRYID":"039","公共部分":"22.0440","设备管理":"0","班组管理":"0","运行管理":"-0.20","安全管理":"-0.10"},{"RRYID":"586","公共部分":"33.2670","设备管理":"0","班组管理":"0","运行管理":"-1.50","安全管……

用原生的js可以吧


<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>测试</title>
<style>


</style>



</head>



<body onload="appendTable()">



</body>
<script type="text/javascript">
 var testArray=[{"RRYID":"039","公共部分":"22.0440","设备管理":"0","班组管理":"0","运行管理":"-0.20","安全管理":"-0.10"},{"RRYID":"586","公共部分":"33.2670","设备管理":"0","班组管理":"0","运行管理":"-1.50","安全管理":"-0.20"},{"RRYID":"429","公共部分":"10.7290","设备管理":"0","班组管理":"0","运行管理":"-0.20","安全管理":"0"},{"RRYID":"372","公共部分":"54.4370","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"0"},{"RRYID":"061","公共部分":"29.7760","设备管理":"0","班组管理":"0","运行管理":"-0.20","安全管理":"-0.20"},{"RRYID":"008","公共部分":"19.1960","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"-0.50"},{"RRYID":"0363","公共部分":"27.6040","设备管理":"0","班组管理":"0","运行管理":"-1","安全管理":"0"},{"RRYID":"0521","公共部分":"38.0160","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"0"},{"RRYID":"0163","公共部分":"27.7390","设备管理":"0","班组管理":"0","运行管理":"0","安全管理":"-0.60"}];
 var headArray=[];
 
 function parseHead(oneRow)
 {
for(var i in oneRow)
{
headArray[headArray.length] = i;
}
 }
 
 function appendTable()
 {
     parseHead(testArray[0]);
     var table= document.createElement("table"); 
 var thead = document.createElement("tr");
 for(var count =0;count<headArray.length;count++)
 {
var td = document.createElement("td");
td.innerHTML= headArray[count];
thead.appendChild(td);
 }
 table.appendChild(thead);
 for(var tableRowNo =0; tableRowNo<testArray.length;tableRowNo++)
 {
var tr = document.createElement("tr");
for(var headCount = 0; headCount<headArray.length; headCount++)
{
var cell = document.createElement("td");
cell.innerHTML = testArray[tableRowNo][headArray[headCount]];
tr.appendChild(cell);
}
table.appendChild(tr);
 }
 document.body.appendChild(table);
 }



</script>
</html>


[解决办法]
直接生成HTML就行了

<div id="table1" ></div>
<script>
$.getJSON("d.json", function(json){
  var htmls=['<table>']; 
  htmls.push('<tr>')
  for(var k in json[0]) htmls.push('<td>'+k+'</td>');
  htmls.push('</tr>');
  for(var i=0,L=json.length;i<L;i++){
    htmls.push('<tr>');
    for(var k in json[i]) htmls.push('<td>'+json[i][k]+'</td>'); 
    htmls.push('</tr>');
  }
  htmls.push('</table>');
  $('#table1').html(htmls.join(''));
</script>

 

热点排行
Bad Request.