我有一个 DataTable,我正在尝试通过 ajax 调用来加载数据,但是第一行数据总是会显示:
但在此行下面,已包含加载的 ajax 数据。如何删除“无数据”行并将 ajax 数据加载到该位置?
以下是代码:
<div class="box-body">
<table id="changeTicketsTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>Ticket Number</th>
<th>Description</th>
<th>Risk</th>
<th>Primary CI</th>
<th>State</th>
<th>Planned Start Date</th>
<th>Actual Start Date</th>
<th>Actual End Date</th>
<th>Affected Partners</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Ticket Number</th>
<th>Description</th>
<th>Risk</th>
<th>Primary CI</th>
<th>State</th>
<th>Planned Start Date</th>
<th>Actual Start Date</th>
<th>Actual End Date</th>
<th>Affected Partners</th>
</tr>
</tfoot>
</table>
</div>
DataTable的实现:
<script>
getChangeTicketInformation();
$('#changeTicketsTable').DataTable({
"pageLength": 5,
'paging' : true,
'lengthChange': true,
'searching' : false,
'ordering' : true,
'info' : true,
'autoWidth' : false
});
})
</script>
Javascript用于进行Ajax调用:
function getChangeTicketInformation(){
$.ajax({
type: "GET",
url: "../../get_change_ticket_info",
success: function(data) {
$.each(data, function (i, item) {
$('#changeTicketsTable').find('tbody').append(
'<tr>' +
'<td>' + item.number + '</td>' +
'<td>' + item.short_description + '</td>' +
'<td>' + item.risk + '</td>' +
'<td>' + item.cmdb_ci_display_value + '</td>' +
'<td>' + item.state + '</td>' +
'<td>' + item.start_date + '</td>' +
'<td>' + item.work_start + '</td>' +
'<td>' + item.work_end + '</td>' +
'<td>' + 'FILL IN' + '</td>'
+ '</tr>');
});
}
});
}
ajax
选项的解决方案被发布,我会给这个答案点赞。相反,我担心OP已经接受了这个答案,因为它所做的是将<tbody>
HTML代码烹制出来,这完全是错误的-DataTables可以自己完成这个工作,此外,使用这种方法不会影响表格数据,因此,您不能使用DataTables API的大多数方法。所以,我只能选择踩它。 - Yevhen Horbunkov