Never been to DZone Snippets before?

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

Zebra stripes on table rows using Rails / RHTML (See related posts)

<% @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 on this post

bear454 posts on Nov 13, 2007 at 18:49
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|%>
  <tr <%= i%2==0 ? "class=/"stripe/"" : "" %>>
  ...
  </tr>
<% end %>


And in the css :


table td {background-color: white; }
table td.stripe {background-color: silver; }
</td>
bear454 posts on Nov 13, 2007 at 18:51
Umm, the &lt;/td%gt; in the last line of my css block above is a typo.
Adam12 posts on Jan 07, 2008 at 23:06
Use cycle().

Stylesheet
table tr.rowA {background-color: white; }
table tr.rowB {background-color: silver; }


View
<% @projects.each do |project| -%>
<tr class="<%= cycle('rowA', 'rowB') %>">...</tr>
<% end -%>

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts