Reputation: 37
I getting from server ajax response(json)and I'm trying to dynamically create table rows and append them to existing table (id="notifications")
My response
the require result is something like that:
<table class="table table-striped " id="notifications">
<thead class="thead">
<tr>
<th width="40%" class ="text-white">お知らせ</th>
<th class ="text-white">配信開始日時</th>
<th class ="text-white">配信停止日時</th>
<th class ="text-white">アクション</th>
</tr>
</thead>
<tbody>
<tr>
<td> <a href="#"></a>0a11107b-9b61-4204-9076-e1a9a98afd66</td>
<td>1995-02-27T00:00:00+09:00</td>
<td>1995-02-27T00:00:00+09:00</td>
<td><a href="#"></a>1</td>
</tr>
<tr>
<td><a href="#"></a>0c6c9967-37db-47f1-b256-58d76ab3e732</td>
<td>1995-02-27T00:00:00+09:00</td>
<td>1995-02-27T00:00:00+09:00</td>
<td><a href="#"></a>1</td>
</tr>
</tbody>
</table>
i trying append but i failed . What am I missing ?
$(function(){
var $notifications = $('#notifications')
$.ajax({
type: 'GET',
dataType:"json",
url: 'http://localhost:8000/api/admin/announces',
succces: function(notifications){
$.each(notifications, function(i,announces) {
$notifications. $('<tr>').append(
$('<td>').text(announces.Id),
$('<td>').text(announces.PublicStart),
$('<td>').text(announces.PublicStop),
$('<td>').text(announces.Image_Url),
);
});
}
});
});
Upvotes: 0
Views: 463
Reputation: 142
HTML
Append tr
and td
elements to tbody
<table class="table table-striped">
<thead class="thead">
<tr>
<th width="40%" class ="text-white">お知らせ</th>
<th class ="text-white">配信開始日時</th>
<th class ="text-white">配信停止日時</th>
<th class ="text-white">アクション</th>
</tr>
</thead>
<tbody id="notifications">
<!-- row comes here... -->
</tbody>
</table>
Script
$(function(){
var $notifications = $('#notifications')
$.ajax({
type: 'GET',
dataType:"json",
url: 'http://localhost:8000/api/admin/announces',
succces: function(notifications){
var data = notifications.announces;
$.each(data, function(i) {
$notifications.append('<tr><td>' + data[i].id + '</td>' +
'<td>'+ data[i].PublicStart + '</td>' +
'<td>'+ data[i].PublicStop + '</td>' +
'<td>'+ data[i].Image_Url + '</td>' +
'</tr>');
});
}
});
});
Upvotes: 1
Reputation: 639
The appending bit is wrong. Try something like:
$.each(notification.announces, function() {
var tr = $('<tr><td>' + this.Id + '</td><td>'+ this.PublicStart + '</td><td>' + this.PublicStop + '</td><td>' + this.Image_Url + '</td></tr>';
$('#notifications').append(tr);
});
Upvotes: 0