-1

我有一个像这样的 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"}
]

我想循环它并附加到一个表

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

4

4 回答 4

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>

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

于 2013-08-14T12:25:59.610 回答
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 打印表格

于 2013-08-13T11:19:50.567 回答
0
$.each(response, function(){
    var html = "<tr><td>" + this.pathologie + "</td><td>" + this.mois+ "</td></tr>";
    $('#myTable').append(html);
});

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

于 2013-08-13T11:14:28.653 回答
0
  $.each(response, function(){
       $(table_id).append("<tr><td>" + this.pathologie + "</td><td>" + this.mois+ "</td></tr>");
    });

试试这样!

已编辑!

于 2013-08-13T11:01:53.640 回答