我有以下代碼:jQuery DataTables()返回空數組,destroy()不能在undefined時調用。
function clearAllCourses() {
activeCourseIDs = [ 0 ];
UnloadStudents();
$('#Australia').show();
}
function UnloadStudents() {
var table = $('#studentDrillDownDataTable');
table.hide();
table.DataTable().destroy();
//table.clear().draw();
//table.ajax.reload();
}
在我的應用程序,還有的是選擇要加載到使用jQuery的數據表的表課程的下拉菜單。當選擇的課程數量從某些課程數量變爲無課程數量(零)時,將調用上面的clearAllCourses()
函數,這又調用UnloadStudents()
函數。
在選定的表格對象上調用destroy()
時會出現該問題。以下是錯誤,以及Chrome Dev Tools控制檯的一些輸出以幫助進行調試。上述發生
Uncaught TypeError: Cannot read property 'length' of undefined jquery.dataTables.js:2574
錯誤時的任何數量的下拉變化爲沒有選擇任何課程(零)中選擇課程。錯誤意味着table.DataTable()
返回undefined
,實際上它返回一個空數組(見下文)。
> $('#studentDrillDownDataTable').DataTable();
> []
結果,destroy()
當任何課程在下拉列表中重新選擇不正確執行,因此,出現此錯誤:
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>]
任何想法是怎麼回事,如何解決?
你能提供[小提琴] (http://jsfiddle.net)有完整的例子嗎? – Regent 2014-09-12 17:43:03
jQuery DataTables是一個非常大的圖書館,而且由於我剛剛交給這個巨大的JS應用程序(其他人構建它),我不知道要抓取哪些部分並將其扔進小提琴中。希望我能輕鬆。 – nhuff717 2014-09-12 17:51:14
沒有完整的例子來進行調試,唯一的希望是那些瞭解DataTables的人...... – Regent 2014-09-12 17:53:16