tags:

views:

2600

answers:

3

I have the following HTML table is rendered to my browser.I am creating this table from my ASP.NET codebehind file.

<table Class="tblTradeInCart">
 <tr class="tblCartHeader">
  <td>Item</td>
  <td>Model</td>
  <td> Price</td>
  <td>Delete</td>
 </tr>
 <tr id="tr_15_1">
  <td><img src="dia/images/LGVX9700.jpg" width="50" height="50" /></td>
  <td>LG VX9700</td>
  <td>$ 122</td>
  <td><a href='#' onclick="deleteItem(15,1,'tr_15_1')"><img src='..\Lib\images\NCcross.gif' style='border:0px'></a></td>
 </tr>
 <tr id="tr_11_8">
  <td><img src="dia/images/NOK5610.jpg" width="50" height="50" /></td>
  <td>NOKIA 5610</td>
  <td>$ 122</td>
  <td><a href='#' onclick="deleteItem(11,8,'tr_11_8')"><img src='..\Lib\images\NCcross.gif' style='border:0px'></a></td>
 </tr>
 <tr id="tr_14_9">
  <td><img src="dia/images/NOKN95.jpg" width="50" height="50" /></td>
  <td>NOKIA N95</td>
  <td>$ 91.5</td>
  <td><a href='#' onclick="deleteItem(14,9,'tr_14_9')"><img src='..\Lib\images\NCcross.gif' style='border:0px'></a></td>
 </tr>
</table>

and In my javascript i have the delete function as follows

function deleteItem(modelId,itemindexId, rowId)
{
   $.get("RemoveFromCart.aspx",{ model:modelId,cartItem:itemindexId,mode:"removefromcart",rand:Math.random() } ,function(data)
 { 
    //document.getElementById(rowId).style.display = "none";

    var row=$("#"+rowId);     
   row.fadeOut(1000);

});

}

But when i call the deleteItem function, I am not getting the fading Effect.Its simply hiding the row like the display="none".

Can any one guide me how to fix this ?

A: 

These two things are different:

  • Hiding the row, either by hide(), fadeOut(), slideUp(), applying a class, setting a CSS value or by way of an animation; and
  • removing the element from the DOM.

If I read what you say correctly, you want to do both. If so, try this:

function deleteItem(modelId,itemindexId, rowId) {
  $.get("RemoveFromCart.aspx", {
    model: modelId,
    cartItem: itemindexId,
    mode: "removefromcart",
    rand:Math.random()
  }, function(data) { 
    var row=$("#"+rowId);     
    row.fadeOut(1000, function() {
      row.remove();
    });
  });
};

Basically this is saying:

  • GET RemoveCart.aspx on the server with the given parameters;
  • When that function returns, start fading the row out over a period of one second;
  • When that fadeOut is complete, delete it from the DOM.
cletus
Unfortunately, I am not getting the FadeEffect. I used the same code above
Shyju
+9  A: 

There is a problem in jQuery when hiding trs. This is the current workaround until they do something similar in the core, if they decide to.

row.find("td").fadeOut(1000, function(){ $(this).parent().remove();});

This basically hides the tds in the row, instead of the actual row. Then it removes the row from the DOM. It works in all browsers I believe. You could target IE specifically though if needed.

Jab
Thanks Jab. This worked fine for me. Thanks alot.Hats off to jQuery Team too .WE love jQuery
Shyju
How can i Show a Redcolor glow on the cell for 1/2 seconds before the TableCell fadesout ?
Shyju
I don't know what you mean by glow, but you could lookup the CSS method on jquery to add a red border to the cell. Use setTimeOut to execute the hiding of the cell 2000ms later.
Jab
This is the correct solution but it contains a bug... see my post below for more info.
Leather
You could make it run just once by changing the selector. row.find("td:last").fadeOut(....)
Jab
This would only fade out the last td, not all of them. My understanding is that they want the whole row to fade out.
Leather
+2  A: 

Whilst Jab's solution is a the way round the problem it does contain a bug. Specifically your callback function to remove the parent element is going to fire once for every 'td' element in that row, when really it should only fire once for the last one. This can be demonstrated by putting an alert call into the callback, which will be seen once for every td in the row.

I have yet to find a really neat way around this but I ended up with something along the lines of this:

function ShowHideTableRow(rowSelector, show, callback)
{
    var childCellsSelector = $(rowSelector).children("td");
    var ubound = childCellsSelector.length - 1;
    var lastCallback = null;

    childCellsSelector.each(function(i)
    {
        // Only execute the callback on the last element.
        if (ubound == i)
            lastCallback = callback

        if (show)
        {
            $(this).fadeIn("slow", lastCallback)
        }
        else
        {
            $(this).fadeOut("slow", lastCallback)
        }
    });
}

To call this you would use something like this:

ShowHideTableRow("#MyTableRowId",false,function() { // do something else ONCE when the row is hidden or shown... });

NOTE: My version does not remove the row from the dom because I just want to hide/show it but it should be fairly easy to adapt.

Leather
I like this, thank you. +1. To make this easier to use though, I'm going to put it in a plugin (probably two separate ones to show and hide) so I can just call `$('tr#myRow').showTableRow()` or `$('tr#myRow').hideTableRow()`...
Funka
Oh, also, it was interesting that I can fade a TR out perfectly fine in IE8, but not fade it in. (I ended up just changing `fadeIn()` to `show()` and then it seems to work, but I think using this solution will work better for the longer-run.
Funka