Is there something built-in to Rails that handles row color alternating? I specifically want/need to change the class that is applied to every other row. I know how to do this using a counter and checking the modulus of it but I was curious if there was a better, more Rails-like way. Kyle Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Jun 21, 2005, at 5:21 PM, Kyle Heon wrote:> Is there something built-in to Rails that handles row color > alternating? I specifically want/need to change the class that is > applied to every other row. > > I know how to do this using a counter and checking the modulus of > it but I was curious if there was a better, more Rails-like way. > > Kyle Heon > kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Kyle- The most Rails way of doing this that I have found is as follows: <% @projects.each_with_index do |project, i| %> <% row_class = i%2 == 0 ? "even" : "odd" %> <tr id="<%= row_class %>"> ...... <% end %> Its a bit better than a counter but pretty much the same idea. -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, 21 Jun 2005, Ezra Zygmuntowicz wrote:> The most Rails way of doing this that I have found is as follows: > > <% @projects.each_with_index do |project, i| %> > <% row_class = i%2 == 0 ? "even" : "odd" %>This puts a class on EVERY row, which is less cool.> <tr id="<%= row_class %>">You shouldn''t be using an id more than once.> ...... > <% end %>Try: <% @projects.each_with_index do |project, i| %> <% row_class = i%2 == 0 ? " class=\"even\"" : "" %> <tr<%= row_class %>"> ...... <% end %> -- _Deirdre web / blog: http://deirdre.net/ yarn: http://fuzzyorange.com cat''s blog: http://fuzzyorange.com/vsd/ "Memes are a hoax! Pass it on!"
.each_with_index allows you to use the array item''s index in the block (see the ''i'' variable in the below example. .each would only have the ''event'' variable in the block. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Kyle Heon Sent: Wednesday, 22 June 2005 10:59 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails] Alternating table row class Thanks everyone. Here is what I came up with: <% @events.each_with_index do |event, i| %> <tr<%= i % 2 == 0 ? " class=\"alternate\"" : "" -%>> ... </tr> <% end %> Just curious, as I''m learning Ruby and Rails, what is the difference between .each and .each_with_index? Kyle Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org -----Original Message----- From: Deirdre Saoirse Moen [mailto:deirdre-mzk6fgDMp2XR7s880joybQ@public.gmane.org] Sent: Tuesday, June 21, 2005 8:50 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Cc: kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org Subject: Re: [Rails] Alternating table row class On Tue, 21 Jun 2005, Ezra Zygmuntowicz wrote:> The most Rails way of doing this that I have found is as follows: > > <% @projects.each_with_index do |project, i| %> <% row_class = i%2 == > 0 ? "even" : "odd" %>This puts a class on EVERY row, which is less cool.> <tr id="<%= row_class %>">You shouldn''t be using an id more than once.> ...... > <% end %>Try: <% @projects.each_with_index do |project, i| %> <% row_class = i%2 == 0 ? " class=\"even\"" : "" %> <tr<%= row_class %>"> ...... <% end %> -- _Deirdre web / blog: http://deirdre.net/ yarn: http://fuzzyorange.com cat''s blog: http://fuzzyorange.com/vsd/ "Memes are a hoax! Pass it on!" _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks everyone. Here is what I came up with: <% @events.each_with_index do |event, i| %> <tr<%= i % 2 == 0 ? " class=\"alternate\"" : "" -%>> ... </tr> <% end %> Just curious, as I''m learning Ruby and Rails, what is the difference between .each and .each_with_index? Kyle Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org -----Original Message----- From: Deirdre Saoirse Moen [mailto:deirdre-mzk6fgDMp2XR7s880joybQ@public.gmane.org] Sent: Tuesday, June 21, 2005 8:50 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Cc: kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org Subject: Re: [Rails] Alternating table row class On Tue, 21 Jun 2005, Ezra Zygmuntowicz wrote:> The most Rails way of doing this that I have found is as follows: > > <% @projects.each_with_index do |project, i| %> <% row_class = i%2 == > 0 ? "even" : "odd" %>This puts a class on EVERY row, which is less cool.> <tr id="<%= row_class %>">You shouldn''t be using an id more than once.> ...... > <% end %>Try: <% @projects.each_with_index do |project, i| %> <% row_class = i%2 == 0 ? " class=\"even\"" : "" %> <tr<%= row_class %>"> ...... <% end %> -- _Deirdre web / blog: http://deirdre.net/ yarn: http://fuzzyorange.com cat''s blog: http://fuzzyorange.com/vsd/ "Memes are a hoax! Pass it on!"