[英]Cannot reinitialize datatables
Hi I'm having the below code where I load the datatable on a click of the button您好我有下面的代码,我点击按钮加载数据表
$("#search").click(function () {
// alert("Video inside");
//$("#Video_Details").dataTable().fnDestroy();
console.log(lat);
$("#Video_Details").DataTable({
// // Ajax for api call
ajax: {
// // api endpoint uri
//urlString: "http://localhost:8080/xx/xx/xx/xx/xx",
url: url,
type: "GET",
data: function (json) {
console.log(json);
return json;
},
},
When I click the second time I'm getting cannot reinitialize error.当我第二次单击时,出现无法重新初始化错误。 Can someone please clarify?
有人可以澄清一下吗?
The simplest solution is to use the DataTables retrieve
option:最简单的解决方案是使用 DataTables
retrieve
选项:
"retrieve": true
From the documentation:从文档中:
if the table has already been initialised, this parameter will cause DataTables to simply return the object that has already been set up
如果表已经被初始化,这个参数将导致 DataTables 简单地返回已经设置好的 object
The reason you get this error is because the DataTable( {...} )
function actually creates and initializes the DataTable - and you can only do this once for each table.您收到此错误的原因是
DataTable( {...} )
function 实际上创建并初始化了 DataTable - 您只能为每个表执行一次。
One alternative is to use the destroy
option - but that is a relatively expensive operation if you are not actually changing any of the initialization options each time you click your button.一种替代方法是使用
destroy
选项 - 但如果您每次单击按钮时实际上都没有更改任何初始化选项,那么这是一个相对昂贵的操作。
Another alternative is to create and initialize the table outside of the button click, and assign it to a variable:另一种方法是在单击按钮之外创建和初始化表,并将其分配给一个变量:
$(document).ready(function() {
var table = $('#example').DataTable( {
...
} );
} );
Now you can use the table
variable.现在您可以使用
table
变量。 This approach means that the table will be initialized and displayed before the first click of the button - which may not be what you want.这种方法意味着表格将在第一次单击按钮之前初始化并显示 - 这可能不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.