简体   繁体   English

根据ajax json响应更新html表

[英]Update html table according to ajax json response

i have a JSON response like below 我有如下的JSON响应

{
   "Parent": "skystar",
   "Children": [
     {"Name":"MW"},
     {"Name":"PR"},
     {"Name":"PV"},
     {"Name":"ST"}
   ]
},
{
       "Parent": "RR",
       "Children": [
         {"Name":"RC"},
         {"Name":"RP"}
       ]
    }

Now i need to bind this to the table. 现在,我需要将此绑定到表。

i tried to call AJAX like below 我试图像下面那样打电话给AJAX

$.ajax({
    url: 'echo/sample.json/',
    success: function (response) {
        var trHTML = '';
        $.each(response, function (i, item) {
            trHTML += '<tr><td>' + item.Children + '</td>';
        });
        $('#records_table').append(trHTML);
    }
});

But i am not able to bind the table 但是我不能绑桌子

what i am doing wrong?? 我做错了什么?

JSFIDDLE 的jsfiddle

one more thing every children has hypher link how to add it 每个孩子还有一件事,有杂种链接如何添加

How to do that in Jquery or javascript?? 如何在Jquery或javascript中做到这一点?

Any help?? 有帮助吗?

Please see following JSFiddle http://jsfiddle.net/s701aduz/ 请参阅以下JSFiddle http://jsfiddle.net/s701aduz/

var jsonData = '[{"Parent":"skystar","Children":[{"Name":"MW"},{"Name":"PR"},{"Name":"PV"},{"Name":"ST"}]},{"Parent":"RR","Children":[{"Name":"RV"},{"Name":"RP"}]}]';

$.ajax({
    url: '/echo/json/',
    type: 'POST',
    data: {
        json: jsonData
    },
    dataType:'json',
    success: function (response) {
        var trHTML = '';
        $.each(response, function (i, item) {
            trHTML += '<tr>';
            $.each(item.Children, function(j, child) {
                trHTML += '<tr><td>' + child.Name + '</td>';
            });
            trHTML += '</tr>';
        });
        $('#records_table').append(trHTML);
    }
});

The problem you had was that item.Children was an array itself so when you included that in the string it would simply output [object Object] , you have to iterate over the children as well. 您遇到的问题是那个item.Children本身就是一个数组,因此当您将其包含在字符串中时,它只会输出[object Object] ,您还必须遍历子[object Object] Also make a row for each element in the response and a td for each child, or at least I'm guessing that's what you were intending. 还要为响应中的每个元素排一行,并为每个孩子做一个td,或者至少我猜这就是您想要的。

Demo: http://jsfiddle.net/tqyn3/336/ 演示: http//jsfiddle.net/tqyn3/336/

var jsonData = '[{"Parent":"skystar","Children":[{"Name":"MW"},{"Name":"PR"},{"Name":"PV"},{"Name":"ST"}]},{"Parent":"RR","Children":[{"Name":"RV"},{"Name":"RP"}]}]';

$.ajax({
    url: '/echo/json/',
    type: 'POST',
    data: {
        json: jsonData
    },
    dataType:'json',
    success: function (response) {
        var trHTML = '';
        $.each(response, function (i, item) {
           //alert(item.children);
            $.each(item.Children, function(j, child) {
                trHTML += '<tr><td>' + child.Name + '</td>';
            });

        });
        $('#records_table').append(trHTML);
    }
});
var json_data = $.parseJSON(response);
    var table = $('<table>');
    $.each(json_data,function(index,obj){
        var tr = $('<tr>');
        $.each(obj.Children,function(_index,_children){
            var td = $('<td>');
            $(td).text(_children.Name);
            $(tr).append(td);
        })

         $('#records_table').append(tr);
    });
var jsonData = [{"Parent":"skystar","Children":[{"Name":"MW"},{"Name":"PR"},{"Name":"PV"},{"Name":"ST"}]},
{"Parent":"RR","Children":[{"Name":"RV"},{"Name":"RP"}]}
];

$.each(jsonData, function (i, item) {
    if(i == 0) {
        $('#records_table').find('th:first').html(item.Parent);
    } else {
        $('#records_table').find('th:last').after('<th>'+ item.Parent +'</th>');
    }

    $.each(item.Children, function(j, child){
        if(i==0) {
            var tr = $('<tr />');
            var td = $('<td />');
            $(td).text(child.Name);
            $(tr).append(td);
            $('#records_table').append( tr );
        } else {
            var td = $('<td />');
            $(td).text(child.Name);
            $('#records_table').find('tr:eq('+(j+1)+')').append( td );
        }
    })
});

Check it will help you http://jsfiddle.net/tqyn3/338/ 选中它会对您有帮助http://jsfiddle.net/tqyn3/338/

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

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