Cannot read property 'node' of undefined table.on( 'select deselect', function ()
Cannot read property 'node' of undefined table.on( 'select deselect', function ()
I am using the table.on( 'select deselect', function () to set a button to enabled .
It work the first time the AJAX data loads, however when I reload the page with new AJAX data, I receive this error "Cannot read property 'node' of undefined"
What am I missing?
table.on( 'select deselect', function () {
var selectedRows = table.rows( { selected: true } ).count();
table.button( 0 ).enable( selectedRows > 0 );
});
This question has an accepted answers - jump to answer
Answers
There is not enough information to help. Can you post a link to your page or a test case so we can understand the code flow and help debug?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Kevin, The application uses PHP to populate the table. Not sure how to do this in live.datatables.net . Would the JS be enough for you to look at the code? If so I can add it with Markdown
Sure you can post your code. Someone may be able to spot something.
How are you reloading the page? Is it a full page reload or just reloading the table data. If you are just reloading the table data then you will either need to use something like
rows().invalidate()
to refresh the Datatables data cache or reinitialize Datatables.Kevin
Just reloading the table. I have a datePicker when onChange runs the reloadReportTable function on line 149 where I clear and destroy the table before reloading.
Would I put the rows().invalidate() after that?
Here is the JS .
I built a test case for you.
http://live.datatables.net/turanowo/1/edit
It uses the server side base code as described in this technote. I copied the portions of your code to replicate the issue. The problem is that each time
loadTable()
is called a newselect deselect
event handler is created without removing the old one. After executingreloadReportTable()
there are now two events. The first one runs but is looking for the reference to the old Datatable that is destroyed.I added the statement
$("#example").DataTable().off( 'select deselect');
and now it works as expected. Comment you this line to see your problem reproduced.Kevin
Kevin thank you so much for the effort. You are a wizard! Kind regards and keep up the good work
Hi Kevin,
You save my day. ^_^
Really appreciate your effort.
Thank you