Hi all, Not really sure what to put in the title so hope people still open this! What I want to do is produce something like this Col1 Col2 Col3 data1 data2 data3 data4 data5 data6 data7 data8 data9 data.. data.. data.. data.. data.. data.. data-n data-n+1 data-n+2 I can get the data back from my db happily, but I cant for the life of me work out how to actually display it. Single column is easy, but anything else has me stumped. Can anyone help? Thanks Jonathan
I''d normally have an iterator that increments each loop and if it''s divisible by 3 print a closing tr tag and start another table row. other than that print every value inside a table cell. Ross On 7/20/06, Jonathan Gill <jonathan.gill@securecirt.com> wrote:> Hi all, > > Not really sure what to put in the title so hope people still open this! > > What I want to do is produce something like this > > Col1 Col2 Col3 > data1 data2 data3 > data4 data5 data6 > data7 data8 data9 > data.. data.. data.. > data.. data.. data.. > data-n data-n+1 data-n+2 > > I can get the data back from my db happily, but I cant for the life of > me work out how to actually display it. Single column is easy, but > anything else has me stumped. > > Can anyone help? > > Thanks > > Jonathan > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ross Riley riley.ross@gmail.com
Ross Riley wrote:> I''d normally have an iterator that increments each loop and if it''s > divisible by 3 print a closing tr tag and start another table row. > other than that print every value inside a table cell. > > Rossif @results contains ur class objects: <table><% @results.each do |obj| -%> <tr> <td><%= obj.col1 %></td> <td><%= obj.col2 %></td> <td><%= obj.col3 %></td> </tr> <% end -%></table> -- Posted via http://www.ruby-forum.com/.
Yes but that only works if you are doing one table row per db row. I thought the original question implied that he was iterating through a DB and every third DB row would drop to another table row. Ross On 7/20/06, Jean-Etienne <etienne.durand@woa.hu> wrote:> Ross Riley wrote: > > I''d normally have an iterator that increments each loop and if it''s > > divisible by 3 print a closing tr tag and start another table row. > > other than that print every value inside a table cell. > > > > Ross > > if @results contains ur class objects: > > <table><% @results.each do |obj| -%> > <tr> > <td><%= obj.col1 %></td> > <td><%= obj.col2 %></td> > <td><%= obj.col3 %></td> > </tr> > <% end -%></table> > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ross Riley riley.ross@gmail.com
Ross Riley wrote:> Yes but that only works if you are doing one table row per db row. I > thought the original question implied that he was iterating through a > DB and every third DB row would drop to another table row. > > RossSomething like that? <table><tr><% @results.each_with_index do |obj, i| -%> <td><%= obj.col1 %></td> <td><%= obj.col2 %></td> <td><%= obj.col3 %></td> <%= "</tr><tr>" if 2 == i.modulo(3) -%> <% end -%></tr></table> -- Posted via http://www.ruby-forum.com/.
Yes that''s what I was thinking.... On 7/20/06, Jean-etienne Eti <etienne.durand@woa.hu> wrote:> Ross Riley wrote: > > Yes but that only works if you are doing one table row per db row. I > > thought the original question implied that he was iterating through a > > DB and every third DB row would drop to another table row. > > > > Ross > > Something like that? > > > <table><tr><% @results.each_with_index do |obj, i| -%> > <td><%= obj.col1 %></td> > <td><%= obj.col2 %></td> > <td><%= obj.col3 %></td> > <%= "</tr><tr>" if 2 == i.modulo(3) -%> > <% end -%></tr></table> > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ross Riley riley.ross@gmail.com
Thanks to both of you! Saved me hours of head scratching! What I ended up with is the following, <table><tr><% @results.each_with_index do |obj, i| -%> <td><%= obj.columnvalue%></td> <%= "</tr><tr>" if 2 == i.modulo(3) -%> <% end -%></tr></table> Which gives me the following: record1.colvalue record2.colvalue record3.colvalue record4.colvalue record5.colvalue record6.colvalue record7.colvalue record8.colvalue record9.colvalue Thanks once again for pushing me in the right direction. Jonathan On Thu, 2006-07-20 at 09:43 +0100, Ross Riley wrote:> Yes that''s what I was thinking.... > > On 7/20/06, Jean-etienne Eti <etienne.durand@woa.hu> wrote: > > Ross Riley wrote: > > > Yes but that only works if you are doing one table row per db row. I > > > thought the original question implied that he was iterating through a > > > DB and every third DB row would drop to another table row. > > > > > > Ross > > > > Something like that? > > > > > > <table><tr><% @results.each_with_index do |obj, i| -%> > > <td><%= obj.col1 %></td> > > <td><%= obj.col2 %></td> > > <td><%= obj.col3 %></td> > > <%= "</tr><tr>" if 2 == i.modulo(3) -%> > > <% end -%></tr></table> > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
Did you try in_groups_of ? Check out http://weblog.rubyonrails.com/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of for a short tutorial. -- Posted via http://www.ruby-forum.com/.
Hello Jonathan,> Thanks to both of you! Saved me hours of head scratching! > > What I ended up with is the following, > > <table><tr><% @results.each_with_index do |obj, i| -%> > <td><%= obj.columnvalue%></td> > <%= "</tr><tr>" if 2 == i.modulo(3) -%> > <% end -%></tr></table>You can also use Array#in_groups_of : <table> <% @results.map(&:columnvalue).in_groups_of(3, fill_with='' '') do |group| -%> <tr class="<%= cycle(''even'', ''odd'') %>"> <% for col in group -%> <td><%= col %></td> <% end -%> </tr> <% end -%> </table> There will be "<td> </td>" code that will pad your table if the size of @results is not a multiple of 3. -- Jean-Fran?ois. -- ? la renverse.
oomtuinstoel wrote:> Did you try in_groups_of ? > > Check out > http://weblog.rubyonrails.com/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of > for a short tutorial.Very nice! -- Posted via http://www.ruby-forum.com/.
Ok so I''m a newbie, and I''m confused! I want to also disply the reulst in columns, nut only two. I begin my loop with: <% for @post in @posts %> <div class="picture" <%= @post.picture %> </div> <% end %> How can I get this to display in two colums rather than one? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Here is what i got so far. <table> <% (@post = Post.find(:all)).to_a.in_groups_of(3) do |group| %> <tr> <% group.each do |title| %> <td><%= title %></td> <% end %> </tr> <% end %> </table> title = column value in db. I get in return the right table div''s, but no info only hashes. It looks like this when render: ### # So I know the columns are working, but how do I get it to display the data? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---