Animating table rows in the browser is problematic. You see, they aren’t block elements and as such don’t have a height or width property. Instead they take their constraints from the content inside them, and the elements that contain them. For rows this typically means they’re constrained by the containing table, and filled by the contained columns.

Today I wanted to slide a row up, and then when it had finished sliding I wanted to remove it from the DOM. Essentially giving it a nice effect when something is deleted.

Given that the height of a row is controlled by it’s content, I figured the easiest way to do this would be to wrap all of the content inside each column with a block element, in this case a div, and then resize those.

jQuery makes this extremely easy :

var el = $(options.element_prefix + id);
el.children("td").each(function() {
    $(this).wrapInner("< div />").children("div").slideUp(function() {el.remove();})
});

NOTE : The div tag in the wrapInner() is malformed because it won’t display properly otherwise. Please remove the space between the opening bracket and ‘div’.

It’s all pretty easy to understand. Essentially my root element is a row, and so for each td in that row wrap it’s content in a div. Then for the child divs in each td, run the slideUp() method. The callback in the slideUp() method says after the animation is done, remove the row. Given the speed of computers these days, no one will notice that the last few columns quite likely just vanish instead of complete their animation.

Posted in AuroraCMS, Code, Product Reviews, Tips and Tricks at December 11th, 2008. Trackback URI: trackback
Tags: , , ,

4 Responses to “Animating Table Rows with jQuery”

  1. December 23rd, 2008 at 1:46 am #Bryan

    wrapInner is an addon? or in jquery - I’m getting errors

  2. December 23rd, 2008 at 9:36 am #aaron

    Please use a div tag in the wrapInner() call. Unfortunately I couldn’t put the div tag into my code and have it display correctly.


    ...wrapInner('< div />‘)…

    wrapInner() is a native jQuery method

  3. January 6th, 2009 at 9:43 am #Daniel

    Can you post a simple demo of this?

    Daniel

  4. January 8th, 2009 at 7:44 pm #aaron

    We’re currently procuring a domain on which to put demo code that may be required in situations such as this. When that’s done I’ll make sure to put something up and update the blog.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>