tags:

views:

310

answers:

2

I have a table. Each row is a record. If you click on a row (record), via jquery, I create a new TR below it, and then insert a new table (related sub-set of data pertaining to the above record) via AJAX. This is first loaded and then I expand the TR in a nice animated fashion.

It looks good.

Now that it's loaded and expanded, I want to easily toggle this TR so one can collapse them and expand them without reloading the content via AJAX.

The obvious/simplest option is to change the event handler for the trigger and give it a TOGGLE event.

Functionally, this works. Aesthetically, though, it's a completely mess.

When I first .toggle to close, the content of the new TR does collapse nicely, but the rest of the parent table doesn't follow until the TR is completely collapsed, then the parent table quickly collapses to fill the gap. This is jarring.

Then, on the next .toggle to open, the child table animates outside of the parent table, then quickly collapses its width. Also very jarring and now I'm left with a very narrow table compared to its parent.

If I omit the animation from the toggle event, it works fine, albeit without the nice transition. I'm guessing this is perhaps more CSS related the jQuery related? Any pointers as to what I might need to declare CSS-wise to keep this erratic visual behavior from happening?

EDIT: per matt's request, markup/script:

<!-- tr of parent table -->
<tr>
    <td><a href="" class="expandable-expanded">link</a></td>
    <td></td>
    <td></td>
</tr>
<!-- this is the tr I create via jQuery and then want to toggle via above link -->
<tr class="expandedContent">
    <td colspan="3">
        <div class="loadedContent" style="display: block;"><div>
        ... my nested table is here ...
        </div></div>
    </td>
</tr>

The jquery I use for the toggle:

$("td a.expandable").click(function(){
    //jquery here that does the initial creation of the TR and loading of the AJAX...

    // after loading ajax, remove the event
    $(this).unbind('click');
    // and then attach the new toggle event
    $(this).closest("tr").next("tr.expandedContent").toggle("slow");

    return false;
});
A: 

Try to collapse/expand nested table rather it's container.

RayZ
Hmm...perhaps this is a clue. To test, instead of toggling, I added a red border.This will add a red border to the TD of the TR I created:$(this).closest("tr").next("tr.expandedContent").children("td").css("border","1px solid red");However, if I try to access the DIV in that TD, it does not seem to work:$(this).closest("tr").next("tr.expandedContent").children("td div.loadedContent").css("border","1px solid red");odd.
DA
$(this).closest("tr").next('.expandedContent').children('td').css("border","1px solid red");
RayZ
Ray: that works, as it's adding a border to the TD. It's when I try to reference the DIV within that it doesn't work (as a route to determine if just collapsing the contents of the TR would work, though it likely won't as semantically and visually, I'm then left with an empty TR)
DA
A: 

Animations on table (and tr) elements (especially height) are flaky, at best. Try writing your markup using div's and whatnot and you'll have much better luck.

thenduks
damn. Well, at least that's a potential explanation as to what's going on. DIVs won't make sense in this scenario, though, as it it truly tabular data. I think we'll have to live with the simple toggle sans any animation for now.
DA
Perhaps your nested table could be made into at least a div _containing_ another table, and then you animate that? The point is just that you shouldn't bother animating tables and their rows themselves, but rather containers that house them
thenduks
thenduks it is (see example above). The problem with doing that is you end up with an empty tr which has it's own CSS issues. But, I could maybe get around that...
DA