What''s the best method for swapping two table rows? For instance, assume you have the following table: <table> <tr> <td>1</td> <td><img src="down.gif"></td> </tr> <tr> <td>2</td> <td><img src="up.gif"></td> </tr> </table> What I want to do is swap the two table rows when someone clicks an image. I thought for sure that things would be golden when I started looking at the Insertion.Before object, but so far it''s yielded no joy. Thanks all, - Dash - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
David Dashifen Kees wrote:> What''s the best method for swapping two table rows? For instance, > assume you have the following table: > > <table> > <tr> > <td>1</td> > <td><img src="down.gif"></td> > </tr> > <tr> > <td>2</td> > <td><img src="up.gif"></td> > </tr> > </table> > > What I want to do is swap the two table rows when someone clicks an > image. I thought for sure that things would be golden when I started > looking at the Insertion.Before object, but so far it''s yielded no joyYou need only use the native javascript method insertBefore(): tbody.insertBefore(secondTr,firstTr); With insertBefore(), when the DOM node given in the first argument is already in the document tree it is simply moved. Full working example below. --Ken working example: <table> <tbody id="tbody"> <tr id="first"> <td>1</td> <td><img src="down.gif"></td> </tr> <tr id="second"> <td>2</td> <td><img src="up.gif"></td> </tr> </tbody> </table> <script> function $(id) { return document.getElementById(id); } $(''tbody'').insertBefore($(''second''),$(''first'')); </script> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
The insertion class seems like a lot of overhead. Why not just use the DOM methods? //assumes t1, t2; not tested var t2sib = t2.nextSibling; t1.parentNode.replaceChild(t2, t1); t2sib.parentNode.insertBefore(t1, t2sib); TAG On Apr 25, 2007, at 9:29 AM, David Dashifen Kees wrote:> > What''s the best method for swapping two table rows? For instance, > assume you have the following table: > > <table> > <tr> > <td>1</td> > <td><img src="down.gif"></td> > </tr> > <tr> > <td>2</td> > <td><img src="up.gif"></td> > </tr> > </table> > > What I want to do is swap the two table rows when someone clicks an > image. I thought for sure that things would be golden when I started > looking at the Insertion.Before object, but so far it''s yielded no > joy. > > Thanks all, > - Dash - > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I was trying insertBefore but it didn''t seem to be working. I''ll have to go back and see what I did wrong. Thanks ;) - Dash - Ken Snyder wrote:> David Dashifen Kees wrote: > >> What''s the best method for swapping two table rows? For instance, >> assume you have the following table: >> >> <table> >> <tr> >> <td>1</td> >> <td><img src="down.gif"></td> >> </tr> >> <tr> >> <td>2</td> >> <td><img src="up.gif"></td> >> </tr> >> </table> >> >> What I want to do is swap the two table rows when someone clicks an >> image. I thought for sure that things would be golden when I started >> looking at the Insertion.Before object, but so far it''s yielded no joy >> > You need only use the native javascript method insertBefore(): > > tbody.insertBefore(secondTr,firstTr); > > With insertBefore(), when the DOM node given in the first argument is > already in the document tree it is simply moved. Full working example > below. > > --Ken > > > working example: > <table> > <tbody id="tbody"> > <tr id="first"> > <td>1</td> > <td><img src="down.gif"></td> > </tr> > <tr id="second"> > <td>2</td> > <td><img src="up.gif"></td> > </tr> > </tbody> > </table> > > <script> > function $(id) { > return document.getElementById(id); > } > $(''tbody'').insertBefore($(''second''),$(''first'')); > </script> > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Tom; this showed me what I had wrong. I didn''t use the parentNode of the row and, instead, grabbed a reference to the table object forgetting about the tbody that is added. I switch my code to use parentNode, as you did below, and now it works perfectly. - Dash - Tom Gregory wrote:> The insertion class seems like a lot of overhead. Why not just use > the DOM methods? > > //assumes t1, t2; not tested > var t2sib = t2.nextSibling; > t1.parentNode.replaceChild(t2, t1); > t2sib.parentNode.insertBefore(t1, t2sib); > > > TAG > > On Apr 25, 2007, at 9:29 AM, David Dashifen Kees wrote: > > >> What''s the best method for swapping two table rows? For instance, >> assume you have the following table: >> >> <table> >> <tr> >> <td>1</td> >> <td><img src="down.gif"></td> >> </tr> >> <tr> >> <td>2</td> >> <td><img src="up.gif"></td> >> </tr> >> </table> >> >> What I want to do is swap the two table rows when someone clicks an >> image. I thought for sure that things would be golden when I started >> looking at the Insertion.Before object, but so far it''s yielded no >> joy. >> >> Thanks all, >> - Dash - >> >> >> > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---