Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...