[英]How do I reinitialise a Datatable?
I have a page with notifications on it which I want to refresh every 10 seconds, but I don't want to reload the entire page, just the reload the div that contains the notifications. 我有一个带有通知的页面,我想每10秒刷新一次,但是我不想重新加载整个页面,而只是重新加载包含通知的div。 The notifications themselves are in a datatable, which is initialised when the page is loaded, but I've been unable to get it to reinitialise the datatable when the div is reloaded.
通知本身在数据表中,该数据表在加载页面时进行了初始化,但是在重新加载div时,我无法获取它来重新初始化数据表。 The div reloads fine but is then just a normal table.
div重新加载正常,但是只是一个普通表。 Here is my code:
这是我的代码:
$(document).ready(function () {
loadNotificationsTable();
});
setInterval(reloadNotificationsTable, 10000);
function reloadNotificationsTable() {
$('#NotificationsTable').dataTable().fnDestroy();
$("#notificationsPlaceholder").load(location.href + " #notificationsPlaceholder>*", "");
$("#notificationsPlaceholder").ready(function () {
loadNotificationsTable();
});
};
function loadNotificationsTable() {
$('#NotificationsTable').dataTable({
ajax: "data.json",
"bLengthChange": false,
'iDisplayLength': 1000,
"bSort": false,
"bFilter": false,
"sDom": 'ft<"bottom"ilp>',
"bDestroy": false,
"bPaginate": false,
"bInfo":false
});
};
Is anybody able to help me and show me what I'm doing wrong? 有人可以帮助我,告诉我我做错了什么吗?
Try re-initializing the datatable after load
has finished in a callback: 在回调中完成
load
后,尝试重新初始化数据表:
$("#notificationsPlaceholder").load(location.href + " #notificationsPlaceholder>*",function(data){
loadNotificationsTable();
});
Also add this to your initialization code since you want to replace the table 还要将其添加到初始化代码中,因为您要替换表
https://legacy.datatables.net/ref#bDestroy https://legacy.datatables.net/ref#b销毁
"bDestroy": true,
If you need to refresh the table at a regular interval of time then use this: 如果您需要定期刷新表,请使用以下命令:
$('#table_id').DataTable().ajax.reload();
Use this code with setTimeout() function to refresh the datatable at a regular interval of time. 将此代码与setTimeout()函数一起使用,可以定期刷新数据表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.