简体   繁体   English

如何在表格列中明智地表示JSON响应

[英]How to Represent JSON response in a table column wise

I am using the following code to receive a json as response and represent them in a table. 我正在使用以下代码来接收json作为响应并将其表示在表中。

$(document).ready(function() {
        var settings = {
            "async" : true,
            "crossDomain" : true,
            "url" : "Parse",
            "method" : "GET",
            "headers" : {
                "content-type" : "application/json"
            },
            "processData" : false
        }

        $.ajax(settings).done(function(jsnData) {

            var obj = JSON.parse(jsnData);
            for (var i = 0, len = obj.length; i < len; i++) {
                var j=0;
                var trd="";
                trd +="<tr>";
                trd+="<td>"
                + obj[i].word
                trd+= " </td>";
                trd+="<td>"
                + "<select class='input-small'><option value=''>"+obj[i].senses+"</option></select>";//obj[i].senses
                trd+= " </td>";
                trd+="</tr>";


                $('#userdata tr:last').after(trd);
            }

        });
    });
    </script>

The userdata is the id o the table I am using to feed this json response. userdata是我用来馈送此json响应的表的ID。

The Json format is Json格式是

[
{
"word": "play",
"senses": ["n1","n2","n3"]
},


 {
"word": "play1",
"senses": ["m1","m2","m3"]

} , { "word": "play2", "senses": ["j1","k2","l3"] }, { "word": "play3", "senses": ["x","ny","z"] }, { "word": "play4", "senses": ["p","q","r"] } ] } , { "word": "play2", "senses": ["j1","k2","l3"] }, { "word": "play3", "senses": ["x","ny","z"] }, { "word": "play4", "senses": ["p","q","r"] } ]

In my table there are two columns,One for word and one for senses,but I want the senses column as drop down.I am getting the output as follow, 在我的表格中有两列,一列代表单词,一列代表感官,但我希望将感官列下拉。我得到的输出如下, 在此处输入图片说明

As you can see,the Option column which I want to use to represent senses,showing the senses for the respected word in one line,eg for word play its senses are showed as n1,n2,n3 .I want them to be shown as drop down,ie for for word play ,drop down values will be n1 n2 and n3 for word play1 ,drop down values will be m1 m2 and m3 . 如您所见,我要用来代表感官的Option列,在一行中显示受尊敬的单词的感官,例如对于单词play其感官显示为n1,n2,n3 。我希望它们显示为下拉,即对于单词play ,下拉值将是n1 n2 and n3对于单词play1 ,下拉值将是m1 m2 and m3

HTML code HTML代码

    <table id="userdata" border="2">
        <tr>
            <td><b>Word</b></td>
            <td><b>Options</b></td>
        </tr>
        <tbody>

        </tbody>
    </table>

I tried hard but was not been able to do it in desired way.any help would be much appreciated. 我很努力,但未能以理想的方式完成。任何帮助将不胜感激。

use this 用这个

 + "<select class='input-small'>";
 for(var k=0; i <obj[i].senses.length;k++){
  trd+="<option value='"+obj[i].senses[k]+"'>"+obj[i].senses[k]+"</option>";
 }
trd+="</select>";

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

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