When invalidating a row fetched by ID: "TypeError: Cannot read property 'src' of undefined"
When invalidating a row fetched by ID: "TypeError: Cannot read property 'src' of undefined"
I'm loading data objects into my DataTable. Then I'm fetching rows by ID, remove them (from the data source) and invalidate them (to update the table).
This works most of the time. But occasionally I'm getting a TypeError: Cannot read property 'src' of undefined
. I believe this has to do with the position of the specific data object I'm trying to delete in the data source (first/last). The line of code in the stacktrace supports this theory.
This JS Bin shows the issue: http://live.datatables.net/fegoteju/6/edit
Click on "Delete Batman" and everything works as intended. Click on "Delete Robin" and you will see the issue (tested in Chrome).
The only somewhat related forum topic I could find is https://www.datatables.net/forums/discussion/19822/error-while-doing-invalidate-the-rows-using-rows-invalidate-having-pagination, but that's from March 2014 and supposedly fixed.
This question has an accepted answers - jump to answer
Answers
Hi,
Thanks very much for the test case here! The issue relates to the use of
row.invalidate()
after the row has been removed. If you remove those invalidations it works as expected.The problem with calling
row().invalidate()
once the row has been removed is that it no longer exists and therefore can't be invalidated.It might look like it works with the Batman row, but that is actually don't something that I think you might not expect. Because the API retains the reference to the row by an index (i.e. an integer) and when you remove a row all other indexes are updated in the table to ensure they are sequential, what is actually happening is that the Robin row is being invalidated there!
So the answer here is not to perform any actions on the row once it has been deleted - they are not required and will fail (potentially in unexpected ways).
What was the intention of calling the
row().invalidate()
method where you did?Regards,
Allan
Hi Allen,
I wish I had a better answer (or excuse), but I'm not sure why I was calling the
invalidate()
. I suspect it was a leftover assumption from making changes to the data source in my head: "when you make changes to the data source and want them to be reflected in the table, you need to call invalidate()". This of course doesn't make sense and I think you're right, I don't need it.I'm also almost certain that I saw this order of method calls in an example somewhere, but I don't find this specific answer anymore and it probably wasn't even an official one in the first place.
So I changed the code the way you described and I can't reproduce the issue anymore in my real project code! Thank you very much for the quick answer!
Cheers,
Henrik