Forum: Rails Spinoffs (closed, excessive spam) Wrapping a table row in <div> tags

Posted by kermit (Guest)
on 2008-07-02 02:24
(Received via mailing list)
Hi,

Hopefully a quick example will illustrate what I'm trying to do.

<table>
  <tr>
    <td>test</td>
    <td>test</td>
  </tr>
  <tr>
    <td>row 2</td>
    <td>row 2</td>
  </tr>
  <tr>
    <td>row 3</td>
    <td>row 3</td>
  </tr>
</table>

Using AJAX, I'd like to be able to replace an entire <tr> block, for
example to give:

<table>
  <tr>
    <td>test</td>
    <td>test</td>
  </tr>

</table>
<p>This is dynamic content</p>
<table>

  <tr>
    <td>row 3</td>
    <td>row 3</td>
  </tr>
</table>

I (wrongly) assumed the solution would just be:

<table>
  <tr>
    <td>test</td>
    <td>test</td>
  </tr>
  <div id='mycontent'>
  <tr>
    <td>row 2</td>
    <td>row 2</td>
  </tr>
  </div>
  <tr>
    <td>row 3</td>
    <td>row 3</td>
  </tr>
</table>

...  then use AJAX to replace the contents of the 'mycontent' div.
Unfortunately when this renders (in Firefox anyway), the div is placed
outside of the table (above it), so any content written to the div
also appear outside of the table (I confirmed the position of the div
by putting a border around it). Perhaps this is to be expected since
in standard HTML the div shouldn't be where it is.

So my question is how I can achieve this replacement of the table row

Best,
Mark
Posted by Gareth Evans (Guest)
on 2008-07-02 02:45
(Received via mailing list)
1) get a reference to your tr (for example using
table.select('tr:nth-child(4)').reduce() )
2) use tr.insert({before: new Element('tr').insert(new
Element('td').update('hai')) }); //or variant of insert with innerhtml
content from ajax request
3) tr.remove();
Posted by kangax (Guest)
on 2008-07-02 03:26
(Received via mailing list)
Gareth,
I believe #replace should be able to do this just fine.

-- kangax
Posted by Gareth Evans (Guest)
on 2008-07-02 03:27
(Received via mailing list)
oh yeah, it would.
I used remove/insert myself for this pattern but replace would work just 
as
well.
Posted by RobG (Guest)
on 2008-07-02 05:16
(Received via mailing list)
On Jul 2, 10:24 am, kermit <t...@linuxbox.co.uk> wrote:
> Hi,
>
> Hopefully a quick example will illustrate what I'm trying to do.
>
[...]
> <table>
>   <tr>
>     <td>test</td>
>     <td>test</td>
>   </tr>
>   <div id='mycontent'>

Replace that with a tbody element.

--
Rob
This topic is locked and can not be replied to.