Is there a callback that lets me take an action on old rows before they are removed from the DOM?
Is there a callback that lets me take an action on old rows before they are removed from the DOM?
jeffreydwalter
Posts: 10Questions: 0Answers: 0
I have a dataTable that has select dropdowns in some of the table cells. I'm using the jQuery select2 plugin on those selects, and am seeing memory leaking when I refresh the dataTable. Because dataTables doesn't unbind all of the event handlers and doesn't destroy all the references to the old rows in _fnDraw(), I need some way to do this manually whenever I refresh the table, but I'm not sure about the best way to go about it.
For now, I have hacked _fnDraw() to do the following:
[code]
/* When doing infinite scrolling, only remove child rows when sorting, filtering or start
* up. When not infinite scroll, always do it.
*/
if ( !oSettings.oScroll.bInfinite || !oSettings._bInitComplete || oSettings.bSorted || oSettings.bFiltered )
{
while( (n = oSettings.nTBody.firstChild) )
{
// HACK: Remove all event handlers from this row, any select2 objects, and finally call empty() just to be sure.
jQuery(n).off('*').find('div[id^=s2id]').select2('destroy').empty();
oSettings.nTBody.removeChild( n );
}
}
[/code]
I would like to be able to hook into a callback that happens on each of the rows to remove so that I can do this kind of cleanup to stop memory leaks.
Is there something like this? If no, what's the best way to do this without hacking dataTables?
For now, I have hacked _fnDraw() to do the following:
[code]
/* When doing infinite scrolling, only remove child rows when sorting, filtering or start
* up. When not infinite scroll, always do it.
*/
if ( !oSettings.oScroll.bInfinite || !oSettings._bInitComplete || oSettings.bSorted || oSettings.bFiltered )
{
while( (n = oSettings.nTBody.firstChild) )
{
// HACK: Remove all event handlers from this row, any select2 objects, and finally call empty() just to be sure.
jQuery(n).off('*').find('div[id^=s2id]').select2('destroy').empty();
oSettings.nTBody.removeChild( n );
}
}
[/code]
I would like to be able to hook into a callback that happens on each of the rows to remove so that I can do this kind of cleanup to stop memory leaks.
Is there something like this? If no, what's the best way to do this without hacking dataTables?
This discussion has been closed.
Replies
Until then, you could use the fnPreDrawCallback function. You wouldn't know which rows would be removed and which would be added, but that might be good enough for you?
Allan
In a table where I populate each row with a custom object, I want to keep track of which row index each object is at. So when rows are created I can store the index somewhere but when rows are destroyed I have to remove from my index manually.
If client-side, then I guess you already know which row is going to be removed since you need to call fnDeleteRow or `row().remove()` (in 1.10).
If server-side procession then all rows are always destroyed and fnPreDrawCallback could be used.
Allan