【发布时间】:2016-10-16 18:51:18
【问题描述】:
我有一个带有过滤器的数据表。我每 10 秒使用一次 Ajax 来获取新数据并向表中添加新行。 (表格数据在同一页面中是动态的,不是ajax)
$(function () {
setInterval(checkNew,1000*10);
$( "#orderdate" ).datepicker({
changeMonth: true,
changeYear: true,
yearRange: '2013:2050',
dateFormat:"dd-mm-yy",
showAnim:"fold"
});
var table = $('#data').DataTable({
"lengthMenu": [[-1], ["All"]],
"bSort": false,
"paging": false,
});
$("#status").change(function() { table.column(4).search($(this).val()).draw() });
});
function checkNew(){
$.post('getdata.php',{max: $("#lastid").text().trim() }).done(function (data) {
if( data.trim() !== '' ) {
$("table#data").prepend(data.trim());
//need to draw datatable to filter and search new data added to the table
}
});
}
如您所见,我正在尝试使用新数据更新或绘制表格,以便过滤或搜索它们。如果我搜索新数据,它不会显示,因为它没有加载 DOM。但我无法重绘我尝试过的表格
$("table#data").DataTable().draw(); // new rows are added but can't be searched or filtered
还有
$('table#data').dataTable().fnDraw(); //new rows are not added
那么无论如何如何编辑代码以正确绘制表格以过滤和搜索新添加的数据?
【问题讨论】:
标签: jquery ajax datatables