views:

156

answers:

4

Hello all!

I need to update a specific table row (via tr id="unique_key") after a successful AJAX call.

HTML fragment:

<a name=\"p{$product_id}\" class=\"tr{$product_id}\"></a>
<tr id="p{product_id}" class="item-row">
<td><h3>{product_id}</h3><a rel="facebox" href="ajax_url">{product_name}</a></td>
<td>{image_information}</td>
<td>{image_sortiment}</td>
<td>{product_status}</td>
</tr>

Javascript:

// AJAX Call
success: function(msg){
    $('#p' + prod_id).remove();
    $('.tr' + prod_id).after(msg);
    $('#p' + prod_id + ' a[rel*=facebox]').facebox();
}
...

What happens:

  • The table row removed
  • Anchors grouped into one single row (not before their <tr>'s) so my 'hook' disappears
  • AJAX result inserted over the whole table (after my 'hook' but still a wrong place)

What's wrong with my idea? How can i force jQuery to 'overwrite' the required table row?

+3  A: 

You can't have anchors inside the table but outside the table rows. All content in a table has to be inside the table cells. If you put content outside the cells, browsers try to fix the code so that it's possible to display it, usually by moving it to after the table.

Manipulating content in a table can be tricky, it can act up if you add rows as html chunks rather than row elements. Use the jQuery function to create elements from the string that you get.

var row = $(msg);
$('#p' + prod_id).replaceWith(row);
Guffa
I hoped the invalid html won't be a problem but you confirmed: it is. Thanks for it. Unfortunetely i cannot step through every rows of my table because it's too big for this-i think.
fabrik
@fabrik: You don't have to step through all rows. Each row element has a `rowIndex` property that you can use to know where in the table's `rows` collection the row is.
Guffa
Awesome! Ingenious! Thanks for it! I looked every single page on jQuery Wiki, looks like i wasn't enough watchful. Thanks again!
fabrik
Yeah, `rowIndex` (and `cellIndex`) are plain DOM Level 1 HTML properties. jQuery doesn't really have any analogue to them.
bobince
A: 

Nothing seems wrong with the idea.

Try just replacing the content of each TD inside that TR instead of removing and adding a new one.

Makram Saleh
A: 

Just it is a idea, tryout this

Your tr must be single or many, concate tr product id with constant and with increment value, like this

 <tr id="p{product_id}cnt1" class="item-row">
 <tr id="p{product_id}cnt2" class="item-row">

If you are going to delete means just the pass the tr position with product id and get the proper value to removed and display the tr in the order.

Karthik
This one isn't enough good because i could update the table row any times when user updates the product's details. Your solution works for one update i think. Btw thanks for your comment.
fabrik
+2  A: 

You can not have any non-table elements directly inside <table> but not inside <td> or <th>. Browsers won't render it correctly.

So you have to place anchors in another <tr><td> .. </td></tr>

But if they are there only to remove row and create new one, you can try replaceWith() instead:

$('#p' + prod_id).replaceWith(msg);
Voyta
Sorry i saw Guffa's answer first. Btw i'll upvote your answer. Thanks!
fabrik