循环遍历json对象并附加到表

时间:2013-08-13 10:58:31

标签: jquery json

我有一个像这样的json对象

"table" : [
    {"pathologie": "Sida", "mois": "1", "nb_cas": "0.0000", "RegionID": "1"},
    {"pathologie": "H\u00e9pathite A", "mois": "1", "nb_cas": "0.0036", "RegionID": "1"},
    {"pathologie": "Diphterie", "mois": "8", "nb_cas": "0.0067", "RegionID": "1"},
    {"pathologie": "Tuberculose", "mois": "1", "nb_cas": "0.0079", "RegionID": "1"},
    {"pathologie": "Cholera", "mois": "1", "nb_cas": "0.0356", "RegionID": "1"},
    {"pathologie": "Paludisme", "mois": "1", "nb_cas": "0.0611", "RegionID": "1"}
]

我希望将其循环并附加到表

感谢您的所有答案,但请原谅我不清楚。我想要做的就是表格link。标题必须包含月份,第一列应包含疾病。

4 个答案:

答案 0 :(得分:0)

  $.each(response, function(){
       $(table_id).append("<tr><td>" + this.pathologie + "</td><td>" + this.mois+ "</td></tr>");
    });

尝试这样!

编辑!

答案 1 :(得分:0)

$.each(response, function(){
    var html = "<tr><td>" + this.pathologie + "</td><td>" + this.mois+ "</td></tr>";
    $('#myTable').append(html);
});

也许这会对你有所帮助。你唯一要做的就是完成结构,以便交出你所有的变量。

答案 2 :(得分:0)

<table border=1>
<tr>
    <td>pathologie</td>
    <td>mois</td>
    <td>nb_cas</td>
    <td>RegionID</td>
</tr>
<?php
$json = '{"table":[{"pathologie":"Sida","mois":"1","nb_cas":"0.0000","RegionID":"1"},{"pathologie":"H\u00e9pathite A","mois":"1","nb_cas":"0.0036","RegionID":"1"},{"pathologie":"Diphterie","mois":"8","nb_cas":"0.0067","RegionID":"1"},{"pathologie":"Tuberculose","mois":"1","nb_cas":"0.0079","RegionID":"1"},{"pathologie":"Cholera","mois":"1","nb_cas":"0.0356","RegionID":"1"},{"pathologie":"Paludisme","mois":"1","nb_cas":"0.0611","RegionID":"1"}]}';//get json here as you want

    $data = json_decode($json);
    foreach( $data->{'table'} as $val){
    $pathologie =  $val->pathologie;
    $mois =  $val->mois;
    $nb_cas =  $val->nb_cas;
    $RegionID =  $val->RegionID;
    echo "<tr><td>$pathologie </td>";
    echo "<td>$mois </td>";
    echo "<td>$nb_cas </td>";
    echo "<td>$RegionID </td></tr>";
    }
echo "</table>";
?>

并使用jquery:

$.getJSON("http://url.com/json.php&callback=?", function(data){
var tablecontents = "";
tablecontents ='<table>';
$.each(data, function (index, value) {

    tablecontents += "<tr class='tr'>";
    tablecontents += "<td>"+value["pathologie"]+"</td>";
    tablecontents += "<td>"+value["mois"].toString()+"</td>";
    tablecontents += "<td>"+value["nb_cas"].toString()+"</td>";
    tablecontents += "<td>"+value["RegionID"].toString()+"</td>";
    tablecontents += "</tr>";
}
}); 
document.getElementById("textTable").innerHTML = tablecontents; 
}); 
} 

<div id=textTable style="color:white;"></div>

希望你有个想法..如何使用json的jquery打印表

答案 3 :(得分:0)

这是我的jquery代码

var regionid = $('#region-incidence').val();
    var post_url = "http://locahost/project/report/TauxIncidence/" + regionid;
                    $.ajax({
                       type: "POST",
                       dataType: "json",
                       url: post_url,
                       data :{variable : variable_de_test}, 
                       async:false,
                       success: function(result) 
                      {           
                $.each(result, function(i,row){
                           $("table.table").append("<tr><td>" + result.table[i].pathologie+ "</td><td>" + result.table[i].mois+ "</td><td>"+result.table[i].nb_cas+ "</td></tr>");
    })

                        } //end success

             }); //end AJAX

和控制器代码

$data['table'] = $this->rapport->TauxIncidenceTest($region);
echo json_encode($data);

我希望将结果附加到表中

嗨我正在尝试使用jquery创建数据透视表 代码:

Jquery的

 $.each(result.tabl, function(i,d){
 var html ="<tr>"+
"<th>" +d.pathologie+ "</th>"+
"<td>" +d.nb_cas+"</td>"+
"</tr>";
$('div#myTable table tbody').append(html);

HTML:

<div id="myTable" class="grid_6">
    <table>
    <caption>Taux Incience</caption>
    <thead>
        <tr bgcolor="#d1d1d1">
            <td></td>
            <th scope="col">January</th>
            <th scope="col">February</th>
            <th scope="col">March</th>
            <th scope="col">Aril</th>
            <th scope="col">May</th>
            <th scope="col">June</th>
            <th scope="col">July</th>
            <th scope="col">August</th>
            <th scope="col">Setember</th>
            <th scope="col">October</th>
            <th scope="col">November</th>
            <th scope="col">Décember</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
 </div>

问题是数据仅在1月份的第一列中显示。 8月和其他月份有数据,但没有显示。 我不知道为什么。帮助我