0

helo 我已经努力设计在页面中随机打印的 json 值的样式,这里有 4 个类别,我用 javascript 做的是找到包含许多对象的 json 数组的长度,然后取类型 id,因为它是在所有数组中通用,确定它们是否属于0,1,2,3类别,然后打印json数组中的所有元素等等,这里我想为每个类别创建表并将所有对应的id推送到表行。

var txt = '<?php echo $response ?>';
//alert(txt);
//var jsonData = eval ("(" + txt + ")");
var jsonData = JSON.parse(txt);
for (var i = 0; i < jsonData.account_detail.length; i++) {
var counter = jsonData.account_detail[i];
//console.log(counter.counter_name);
//alert(counter.type);
document.write("<table border="0" width="500>">")
if(counter.type=="0")
    {

      document.write(counter.building_name);
      document.write(counter.org_name);
      document.write(counter.user_name);
      document.write(counter.name);
      document.write(counter.loc_name);
      document.write(counter.email_id);
      document.write(counter.password);

    }
if(counter.type=="1")
    {
        document.write(counter.user_name);
        document.write(counter.name);
        document.write(counter.password);
        document.write(counter.email_id);
    }
    if(counter.type=="2")
        {
            document.write(counter.building_name);
            document.write(counter.org_name);
            document.write(counter.user_name);
            document.write(counter.opr_code);
            document.write(counter.name);
            document.write(counter.loc_name);
            document.write(counter.email_id);
            document.write(counter.password);
        }
        if(counter.type=="3")
            {
               document.write(counter.building_name);
               document.write(counter.org_name);
               document.write(counter.machine_type);
               document.write(counter.activate_status);
               document.write(counter.machine_name);
               document.write(counter.entrance_exit_name);
               document.write(counter.entrance_or_exit);
               document.write(counter.loc_name);
               document.write(counter.activation_code);
            }
}
document.write("</table>")
4

1 回答 1

0

尝试这个:

 for (var i = 0; i < jsonData.account_detail.length; i++) {
var counter = jsonData.account_detail[i];
//console.log(counter.counter_name);
//alert(counter.type);
var tbl_str = '<table border="0" width="500><tr>';
    if(counter.type=="0")
    {
      tbl_str += "<td>"+counter.building_name+"</td>";
      tbl_str += "<td>"+counter.org_name+"</td>";
      tbl_str += "<td>"+counter.user_name+"</td>";
      tbl_str += "<td>"+counter.name+"</td>";
      tbl_str += "<td>"+counter.loc_name+"</td>";
      tbl_str += "<td>"+counter.email_id+"</td>";
      tbl_str += "<td>"+counter.password+"</td>";
    }
    else if(counter.type=="1")
    {
        tbl_str += "<td>"+counter.user_name+"</td>";
        tbl_str += "<td>"+counter.name+"</td>";
        tbl_str += "<td>"+counter.password+"</td>";
        tbl_str += "<td>"+counter.email_id+"</td>";
    }
    else if(counter.type=="2")
    {
            tbl_str += "<td>"+counter.building_name+"</td>";
            tbl_str += "<td>"+counter.org_name+"</td>";
            tbl_str += "<td>"+counter.user_name+"</td>";
            tbl_str += "<td>"+counter.opr_code+"</td>";
            tbl_str += "<td>"+counter.name+"</td>";
            tbl_str += "<td>"+counter.loc_name+"</td>";
            tbl_str += "<td>"+counter.email_id+"</td>";
            tbl_str += "<td>"+counter.password+"</td>";
     }
     else {
               tbl_str += "<td>"+counter.building_name+"</td>";
               tbl_str += "<td>"+counter.org_name+"</td>";
               tbl_str += "<td>"+counter.machine_type+"</td>";
               tbl_str += "<td>"+counter.activate_status+"</td>";
               tbl_str += "<td>"+counter.machine_name+"</td>";
               tbl_str += "<td>"+counter.entrance_exit_name+"</td>";
               tbl_str += "<td>"+counter.entrance_or_exit+"</td>";
               tbl_str += "<td>"+counter.loc_name+"</td>";
               tbl_str += "<td>"+counter.activation_code+"</td>";
            }    
tbl_str += "</tr></table>";    
document.write(tbl_str);    
}
于 2013-04-26T09:22:10.153 回答