DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Zebra Stripes On Table Rows Using Rails / RHTML

06.22.2005
| 13757 views |
  • submit to reddit
        
<% @projects.each_with_index do |project, i| %>
<% row_class = i%2 == 0 ? "even" : "odd" %> 
<tr class="<%= row_class %>">
......
<% end %>

In the CSS:

TR.even { background-color: #f00; }
TR.odd { background-color: #f00; }

etc..    

Comments

Snippets Manager replied on Wed, 2009/07/15 - 3:38pm

Normally when I try to do something assignment | assignments | assignment writing that ends up being this complicated, there is a better, more "railsy" way of doing it that I've missed... can't work out how to do this without doing it within the controller though without adding lots of redundant extra steps. buy assignment | custom assignment | assignment help | online assignment

Snippets Manager replied on Mon, 2008/01/07 - 10:29pm

Use cycle(). Stylesheet table tr.rowA {background-color: white; } table tr.rowB {background-color: silver; } View <% @projects.each do |project| -%> ">... <% end -%>

Snippets Manager replied on Tue, 2007/11/13 - 7:40pm

Umm, the </td%gt; in the last line of my css block above is a typo.

Snippets Manager replied on Tue, 2007/11/13 - 7:40pm

Couple things: 1 - your two css statements have the same color background... 2 - I think there's a DRYer way: <% @projects.each_with_index do |project, i|%> > ... <% end %> And in the css : table td {background-color: white; } table td.stripe {background-color: silver; }