fnDeleteRow does it also delete from the DOM?

fnDeleteRow does it also delete from the DOM?

fecundfecund Posts: 15Questions: 0Answers: 0
edited November 2011 in General
I have an ajax script that removes rows, I'm curious if I call fnDeleteRow should I also call .remove() to completely remove it from existence or does DT do this for me? When I delete a row I never want it back.

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    DataTables will remove it from the DOM, and as long as the row isn't referenced anywhere else, the browser's Javascript garbage collector will delete it from memory. The only thing to watch is if you have assigned events to the TR or any children, since that would mean there is a reference and thus keep the row in memory - if you do assign events, you need to unassign them otherwise you get a memory leak.

    Allan
  • fecundfecund Posts: 15Questions: 0Answers: 0
    I have some elements of the rows linked to .live() events in jQuery. It should release those automatically right?
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    No - events aren't released automatically. However, in this case you probably want to leave your live event as it is, since presumably it will still be being used for other content which is still on the page or will be put on in future? Generally you don't need to worry about memory leaks with live events, although it is worth being aware of what is going on :-).

    Allan
  • GregPGregP Posts: 500Questions: 10Answers: 0
    Just to support allan's answer: the live event is a listener on the document itself. There will only be one instance of it on the page. As you destroy rows and add new ones, you are not touching the live event, which will take up the same amount of memory no matter what.

    Side note: it's a pet recommendation of mine to convince people to start using .delegate() instead of .live(). It's easy to do and will perform better especially on a complex page.
  • fecundfecund Posts: 15Questions: 0Answers: 0
    Yet another followup from jQuery docs:

    "As of jQuery 1.7, .delegate() has been superseded by the .on() method. For earlier versions, however, it remains the most effective means to use event delegation. More information on event binding and delegation is in the .on() method. In general, these are the equivalent templates for the two methods:"
  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited November 2011
    Haha, awesome. Thanks, fecund. I must start recommend using .on(). The poor folks using .live() all the time are going to be even further behind. ;-)

    Just read the docs, and the 'data' parameter could be quite versatile indeed.
This discussion has been closed.