views:

595

answers:

2

Hey guys, I have written some jQuery code to use the "slideUp" animation when deleting rows from a table. In order for the animation to appear smooth, I wrapped the contents of each TD in the row with DIV tags, and called the slideUp function on the DIVs, removing the actual TDs upon completion of the animation. The code for that is as follows:

$("tr[id$=" + rowID + "]").children("td").each(function() {
    $(this).children("div").slideUp(function() {
        $(this).parent().remove();
    });        
});

So far, so good. However, I noticed that this code does not remove the actual TR, only its contents, so I added the following line to remove the TR:

$("tr[id$=" + rowID + "]").remove();

The problem is that after I added the line above, the animation quit working. In other words, the row simply disappears without the nice sliding effect. Does anybody know why this is so and how to get around it?

Thanks in advance for your insights! Victor

+1  A: 

Because you're removing the tr that contains all of the animated elements. No more elements, no more animation. Given that all of the slideUps should be taking the same amount of time, you can probably get away with changing the finish callback from $(this).parent().remove() to $(this).closest('tr').remove().

hobbs
I'm always amazed at the speed of response on this site. Thanks for the quick reply, hobbs. I understand what you mean about the consequences of removing the tr containing the animated elements, but I thought these operations happened in sequence...I guess that is not so. The asynchronous nature of javascript gets me every time! Your trick worked out quite well, by the way. Thanks!
Well, it's really the animation calls (e.g. slideUp) that are asynchronous, because they do their work in timer events... but anyway yeah, they don't wait for completion, so your remove was firing before the animation even got a chance to properly start.
hobbs
+1  A: 

I wrote a jQuery plugin that lets you do this. You can add and remove rows and it doesn't require wrapping your data with a div or anything like that. Check it out at http://www.fletchzone.com/post/jQuery-Unobtrusively-Animated-Add-and-Remove-Table-Rows.aspx

Best,

Fletch

Fletch