tags:

views:

612

answers:

3

I am cloning a hidden table row then populating it and after validation I want to show the row using a jquery effect ... say .show("slow")

var baseRow = $("#tasks tr#baseTaskLine");
var newRow = baseRow.clone();
var lastRow = $("#tasks tr[id^='TaskLine_']" + dayClass + ":last");

var newRowId;
if (lastRow.length == 0) {
   newRowId = "TaskLine_new0";
}
else {
   newRowId = "TaskLine_new" + lastRow[0].rowIndex;
}

newRow.attr("id", newRowId);

:
[populate new row]
:

if (lastRow.length == 0) {
   baseRow.after(newRow);
}
else {
   lastRow.after(newRow);
}
newRow.hide();
:

:
[validate via webservice call]
:
newRow.show("slow");

This does show the row but it appears instantly. I have tried hiding all the <td> elements of the row then showing those and that does seem to work but some strange styles get added to each <td> which interfere with the formatting i.e. style="display: block;"

+8  A: 

This is not going to work this way. Table rows and cells are not meant to be displayed as blocks so the show/fade effects are not going to work on table rows directly.

You could, however, put a <div> in each of the cells, something like this:

<table>
<tr id="row1"><td><div>Cell1:1</div></td><td><div>Cell2:1</div></td></tr>
<tr id="row2"><td><div>Cell1:2</div></td><td><div>Cell2:2</div></td></tr>
</table>

and then to the following:

$('#row2 td div').show('slow');

This will yield the expected behaviour.

DrJokepu
Thanks, this works great on FireFox, however on IE7 it still doen't quite display properly (the bold style that should be inherited from the <tr> class is lost).
Sam Mackrill
You can put style="font-weight:bold;" into each of the divs. Even better, you can create a CSS class like b{font-weight:bold;> and then apply class="b" for each of the divs,
DrJokepu
sorry, that was <div style="font-weight:bold;"> b{font-weight:bold;} and <class="b">. Stackoverflow ate my < > characters.
DrJokepu
Also my HTML entities.
DrJokepu
Yes, that was my point the CSS class on the <tr> is not fully applied to the <div> in IE, I will try adding it specifically
Sam Mackrill
A: 

Could you perhaps animate the height property of the row? I'm not sure if this would work, but it might be a lot simpler than adding extra markup.

<table id="myTable">
    <tbody>
        <tr><td>Row 1,1</td><td>Row 1,2</td></tr>
        <tr><td>Row 2,1</td><td>Row 2,2</td></tr>
    </tbody>
</table>

and this:

// get the row you're after.
var $row = $("#myTable tr:last");
// store its height
var h = $row.height();

$row
    .css("height", 0)    // set the height back to 0
    .animate({
        height : h + "px"    // animate it back to normal.
    }, "slow")
;
nickf
A: 

I wrote a jQuery plugin that lets you do this. You can add and remove rows (with animation) 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