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?

jeffreydwalterjeffreydwalter Posts: 10Questions: 0Answers: 0
edited October 2013 in DataTables 1.9
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?

Replies

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Currently no there isn't. This is the first request for such an event, but I can see it might be useful. Not sure I'll land it for 1.10, but I'll certainly think about it!

    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
  • rewenrewen Posts: 74Questions: 2Answers: 0
    +1 for a row-destroyed callback.

    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.
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Are you using server-side processing or client-side?

    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
This discussion has been closed.