Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

javascript - Programmatically append some td to tr - jQuery AJAX JSON

I've been looking for some way to append td to tr to table using jQuery without knowing anything about my json response, at this point in time I've reached this:

function getClientes(){
    $.ajax({
    url:URL_BASE+"Cliente",
    type:"GET",
    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Basic " + btoa("hola" + ":" + "hola"));
    },
    success: function (data){
            $.each(data,function(index,value){
                $('<tr>').append(
                    $.each(value,function(value,celda)
                    {
                        $('<td>').text(celda);
                    })                      
            ).appendTo('#table');
            })
        }
    });     
}

The problem is that I'm getting my .html output like this:

<table id="table" class="table table-condensed">
   <tr></tr>
   <tr></tr>
</table>

So this is not appending the td to the tr, I do not want to append them mannually I know it is very easy to do it but this is way better for me, please help :D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Replace your code with this::

function getClientes(){
    $.ajax({
    url:URL_BASE+"Cliente",
    type:"GET",
    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Basic " + btoa("hola" + ":" + "hola"));
    },
    success: function (data){
            $.each(data,function(index,value){
                $('tr').append(
                    $.each(value,function(value,celda)
                    {
                        $('td').text(celda);
                    })                      
            ).appendTo('#table');
            })
        }
    });     
}

I think it will work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...