Javascript 如何销毁数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28755144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:20:31  来源:igfitidea点击:

How can I destroy a datatable

javascripthtmldatatabledatatables

提问by johny mile

I have datatable and table have ID example.

我有数据表和表有 ID 示例。

Now I need to destroy datatable and I write:

现在我需要销毁数据表,我写:

$('#example').dataTable().fnDestroy();

but I get:

但我得到:

Uncaught TypeError: Cannot read property 'style' of undefined

未捕获的类型错误:无法读取未定义的属性“样式”

also this I get in console log:enter image description here

这也是我在控制台日志中得到的:在此处输入图片说明

What is the problem here? Why can't I destroy the datatable? How to solve this?

这里有什么问题?为什么我不能销毁数据表?如何解决这个问题?

回答by Red Shift

For latest version of datatables use:

对于最新版本的数据表,请使用:

$('#example').DataTable().destroy();

Refer to this for more: https://datatables.net/reference/api/destroy%28%29

更多信息请参考:https: //datatables.net/reference/api/destroy%28%29

For older versions use as stated by Hobo Sapiens:

对于旧版本,请使用 Hobo Sapiens 所述:

$('#example').DataTable().fnDestroy();

回答by jjwdesign

Here's what eventually worked for me with v1.10.

以下是 v1.10 最终对我有用的内容。

// Define a variable for your dataTable object to use
var reportListDataTable = null;

// Then inside a function/method somewhere...

// Destroy the dataTable and empty because the columns may change!
if (reportListDataTable !== null ) {
    // for this version use fnDestroy() instead of destroy()
    reportListDataTable.fnDestroy();
    reportListDataTable = null;
    // empty in case the columns change
    $('#reportListTableId').empty();
}

// Build dataTable with ajax, columns, etc.
reportListDataTable = $('#reportListTableId').dataTable({
    //... Your dataTables code here
});

回答by Munam Yousuf

what worked for me is destroy the dataTable on click of a insert button rather on the fetch data button. So the button that destroys the dataTable is not recreating the table. And some other function that is called from other button click creates the dataTable. code is simple :

对我有用的是单击插入按钮而不是获取数据按钮来销毁数据表。所以破坏数据表的按钮不是重新创建表。从其他按钮单击调用的其他一些函数会创建数据表。代码很简单:

Button that destroys dataTable:

销毁数据表的按钮:

var otable = $('#claimRecordTable').dataTable();
if (otable != null) otable.fnDestroy();

button that created dataTable:

创建数据表的按钮:

$('#claimRecordTable').dataTable();