我有以下代码:
function clearAllCourses() {
activeCourseIDs = [ 0 ];
UnloadStudents();
$('#Australia').show();
}
function UnloadStudents() {
var table = $('#studentDrillDownDataTable');
table.hide();
table.DataTable().destroy();
//table.clear().draw();
//table.ajax.reload();
}
在我的应用程序中,有一个下拉菜单,可以选择要使用 jQuery DataTables 加载到表中的类(class)。当所选类(class)的数量从一定数量的类(class)变为无类(class)(零)时,会调用上面的 clearAllCourses()
函数,该函数又会调用 UnloadStudents()
功能。
在选定的表对象上调用 destroy()
时会出现问题。以下是错误,以及 Chrome 开发工具控制台的一些输出,以帮助调试。
未捕获的类型错误:无法读取未定义的 jquery.dataTables.js 的属性“长度”:2574
当下拉列表中任意数量的选定类(class)更改为未选择类(class)(零)时,就会出现上述错误。该错误意味着 table.DataTable()
返回 undefined
,实际上返回一个空数组(见下文)。
> $('#studentDrillDownDataTable').DataTable();
> []
因此,destroy()
无法正确执行,因此当在下拉列表中重新选择任何类(class)时,会发生此错误:
Uncaught Error: DataTables warning: table id=studentDrillDownDataTable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3 jquery.dataTables.js:4832
下一个想法可能是table
是否不是已初始化的DataTable
或者table
是否不是它应该的样子。我已经排除了这种可能性,因为这是我选择表格时的输出:
[<table class="table table-datatable table-custom display dataTable" id="studentDrillDownDataTable" width="100%" aria-describedby="studentDrillDownDataTable_info" role="grid" style="display: none; width: 100%;">…</table>]
知道发生了什么以及如何解决它吗?
最佳答案
尝试将 $('#..').DataTable() 更改为 $('#..').dataTable() - 这对于 DT > 1.9 的版本是正确的
关于javascript - jQuery DataTables() 返回空数组,未定义时无法调用 destroy()。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25813843/