Alternating Table Row Colors in Rails

by Melvin Ram on January 5, 2011

A common task in business web applications is displaying tables of content in a spreadsheet style format. A nice treatment that adds a lot of usability to such tables of data is visually separating alternating rows of content.

The easiest way to do this in Rails is to use the cycle function as follows:

<%= cycle("odd", "even") %>

Here’s a more full example:

<tr class="<%= cycle("odd", "even") %>">
  Row of content
</tr>

You could easily use it with any element, such as a div or li.

For more information, please see:
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-cycle

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: