以表格格式显示JSON结果

时间:2020-12-07 21:18:06
 $.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

<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. I could see the response by using chrome Inspect element under network option . Please help me to display the retrieved results in HTML tabular format.

我想以HTML表格格式显示结果,所以我将结果以数组格式传递给Json,但结果不会显示在HTML页面中。我可以通过在网络选项下使用chrome Inspect元素来查看响应。请帮我以HTML表格格式显示检索到的结果。

3 个解决方案

#1


0  

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. So in your js you cannot use data.die_no etc Use this like just I did:

实际上你的PHP代码没有返回键值对。所以在你的js你不能使用data.die_no等使用这就像我做的那样:

data[0][i]

#2


1  

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:

 $.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

#3


0  

You have syntax error: use

您有语法错误:使用

txt += <tr><td>

instead of

txt += tr><td>

after if condition

如果有条件

#1


0  

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. So in your js you cannot use data.die_no etc Use this like just I did:

实际上你的PHP代码没有返回键值对。所以在你的js你不能使用data.die_no等使用这就像我做的那样:

data[0][i]

#2


1  

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:

 $.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

#3


0  

You have syntax error: use

您有语法错误:使用

txt += <tr><td>

instead of

txt += tr><td>

after if condition

如果有条件