2012-09-01 22 views
0
var url = 'json/result.json'; 
$(document).ready(function() { 
    $.getJSON(url, function(data) { 
     $.each(data.video, function(index, video) { 
      var row = $("<tr></tr>");     
      $('#userDataGrid').append('<td>' + video.id + '</td>'); 
      $('#userDataGrid').append('<td>' + video.name + '</td>'); 
      $('#userDataGrid').append('<td>' + video.url + '</td>'); 
      $.each(video.author.data, function(index, author) { 
       $('#userDataGrid').append('<td> Author: ' + author.name_author + '</td>'); 
      }); 
      $('#userDataGrid').append('<br/>'); 
     }); 
    }); 
    $("#userDataGrid p").addClass("selected highlight"); 
    console.log("========================"); 
    //console.log(data); 
    console.log("========================"); 
}); 

回答

0

可以使用wrapAll方法。

$.each(data.video, function(index, video) { 
    // ... 
    $('#userDataGrid > td').wrapAll('<tr></tr>') 
}); 
+0

我喜歡這個wrapAll但其包裝中。每一個記錄,有什麼想法? –

+0

@ ShoaibUd-Din在'each'循環結束時調用它。 – undefined

+0

3 td在一個tr然後3 td在一個tr –

1
$.getJSON(url, function(data) { 
    $.each(data.video, function(index, video) { 
     var row_str= '<tr>';     
     row_str += '<td>' + video.id + '</td>'; 
     row_str += '<td>' + video.name + '</td>'; 
     row_str += '<td>' + video.url + '</td>'; 

      $.each(video.author.data, function(index, author) { 
       row_str += '<td> Author: ' + author.name_author + '</td>'; 
      }); 
     row_str += '<br/>'; 
     row_str += '</tr>'; 
     $('#userDataGrid').append(row_str); 
    }); 
}); 
+0

順便說一句,只要看看這個 - http://stackoverflow.com/questions/171027/add-table-row-in-jquery。這裏有幾種方法 – ddb