Why Datatables is trowing error "Cannot read property 'aDataSort' of undefined" when i destroy old i
Why Datatables is trowing error "Cannot read property 'aDataSort' of undefined" when i destroy old i
cgc
Posts: 3Questions: 1Answers: 0
When my datatable is initialized for the first time it works everything well. Then i need to reinitialize the table to populate with the new data it throws error "Cannot read property 'aDataSort' of undefined". Here is my code:
initDt( pT, pOptions ) {
if ( $.fn.dataTable.isDataTable( pT ) ) {
$( pT ).DataTable().destroy();
let table = $( pT ).DataTable( {
dom: 'Bfrtip',
responsive: true,
paging: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"scrollY": "320px"
} );
} else {
let table = $( pT ).DataTable( {
dom: 'Bfrtip',
responsive: true,
paging: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"scrollY": "320px"
} );
}
}
I've also tried to pass destroy: true
in the options but the error appears the same.
The table is destroyed, when i initialize it again its when the error is being thrown.
How can i solve this?
This discussion has been closed.
Answers
I tried your code here:
http://live.datatables.net/jususoge/1/edit
It seems to work. Interestingly if I use $( pT )
.empty()
after the destroy I get the same error. So it seems thetable
is not properly built before reinitializing. IF this doesn't help then a link to your page or a test case replicating the issue will be needed to help troubleshoot.Kevin
After thinking about it a bit.... If you are removing the
table
you will probably want to destroy() it first to remove all the Datatables listeners etc then remove thetable
.Kevin
Maybe i should have posted this on my question but pT is a string containing
"#myTableId"
.I'm a bit confused, so what should i need to change on my code?
Also, this is what i have included in my html to use datatables:
The Datatable code, as you posted it, is working. I was trying to point out that something is happening elsewhere in your code that is not allowing Datatables to initialize.
How are you adding the new data? What are you doing with the table to remove the old data? Is the new table in correct HTML format?
Without seeing what you are doing outside of the code snippet above there isn't much help we can provide. Can you provide a test case so we can help?
Kevin
After a couple more hours debugging, i've discovered the problem. I had my functions wrapped inside an object like this:
In this case i was just calling the functions like initDt('#myTable');
Then i tested with plain declared methods like this:
And it worked. Not sure why this happened.