简体   繁体   English

以表格格式显示JSON结果

[英]Display JSON result in Table Format

 $.ajax({
     type: 'GET',
     url: 'die_issue_result.php',
     data: {
         vals: die_no
     },
     dataType: "json", //to parse string into JSON object,
     success: function (data) {
         if (data) {
             var len = data.length;
             var txt = "";
             if (len > 0) {
                 for (var i = 0; i < len; i++) {
                     if (data[i].die_no && data[i].status && data[i].location) {
txt += "<tr><td>"+data[i].die_no+"</td><td>"+data[i].status+"</td><td>"+data[i].location+"</td></tr>";
                     }
                 }
                 if (txt != "") {
                     $("#table").append(txt).removeClass("hidden");
                 }
             }
         }
     }

Controller page 控制器页面

$die_no = array();
$status = array();
$location = array();
while ($row = mysql_fetch_array($query)) {
    $die_no[] = $row["die_no"]; // or smth like $row["video_title"] for title
    $status[] = $row["status"];
    $location[] = $row["location"];

}
$res = array($die_no, $status, $location);
echo json_encode($res);

HTML page HTML页面

<p>
    <table id="table" class="hidden">
        <tr>
            <th>die_no</th>
            <th>Status</th>
            <th>Location</th>
        </tr>

I would like to display result in HTML table format, so I have passed my result in array format to Json but the results are not displayed in HTML page. 我想以HTML表格格式显示结果,所以我将结果以数组格式传递给Json,但结果不会显示在HTML页面中。 I could see the response by using chrome Inspect element under network option . 我可以通过在网络选项下使用chrome Inspect元素来查看响应。 Please help me to display the retrieved results in HTML tabular format. 请帮我以HTML表格格式显示检索到的结果。

If you add console.log(data) in your succes response,you can see how the object is structured. 如果在succes响应中添加console.log(data),则可以看到对象的结构。

To access the desired json value you should try data['die_no'][i],data['status'][i],data['location'][i]. 要访问所需的json值,您应该尝试数据['die_no'] [i],data ['status'] [i],data ['location'] [i]。

You can insert the response like this: 您可以像这样插入响应:

<table id="tbl">
</table>

Javascript: 使用Javascript:

 $.ajax({
 type: 'GET',
 url: 'die_issue_result.php',
 data: {
     vals: die_no
 },
 dataType: "json", //to parse string into JSON object,
success: function (data) {
         if (data) {
         var len = data.length;
             if (len > 0) {
                 for (var i = 0; i < len; i++) {                         
                  $('$tbl').append("<tr><td>"+data['die_no'][i]+"</td><td>"+data['status'][i]+"</td><td>"+data['location'][i]+"</td></tr>");                         
                 }                     
             }
         }
}
}); //you missed this in your question

You have syntax error: use 您有语法错误:使用

txt += <tr><td>

instead of 代替

txt += tr><td>

after if condition 如果条件

Use this 用这个

$.ajax({
            type: 'GET',
            url: 'die_issue_result.php',
            data: {
                vals: die_no
            },
            dataType: "json", //to parse string into JSON object,
            success: function (data) {

                if (data) {
                    var len = data.length;
                    var txt = "";
                    if (len > 0) {
                        for (var i = 0; i < len; i++) {
                            if (data[0][i] || data[1][i] || data[2][i]) {
                                txt += "<tr><td>" + data[0][i] + "</td><td>" + data[1][i]  + "</td><td>" + data[2][i]  + "</td></tr>";
                            }
                        }
                        if (txt != "") {
                            $("#table").append(txt).removeClass("hidden");
                        }
                    }
                }
            }
        });

Actually your php code is not returning key value pair. 实际上你的PHP代码没有返回键值对。 So in your js you cannot use data.die_no etc Use this like just I did: 所以在你的js你不能使用data.die_no等使用这就像我做的那样:

data[0][i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM