# Alternate Colors for row blocks

**URL:** <https://www.ruby-forum.com/t/alternate-colors-for-row-blocks/131585>\
**Category:** Rails\
**Created:** [March 11, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/alternate-colors-for-row-blocks/131585 "2008-03-11T17:33:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [March 11, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/alternate-colors-for-row-blocks/131585/1 "2008-03-11T17:33:37Z")

</div>

Hello.

I need to do a twist on the alternate row colors. I looked at cycle(),  
but that appears to be table row specific.

I have a table header. Then I have up to 3 table rows that are grouped  
together in a loop. I’d like to alternate colors per group, not per  
row. If I put the 3 table rows into a separate table, it makes a mess  
out of my table and table header.

Any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [March 11, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/alternate-colors-for-row-blocks/131585/2 "2008-03-11T18:12:03Z")

</div>

On Mar 11, 2008, at 12:33 PM, Becca G. wrote:

> out of my table and table header.
> 
> Any ideas?
> 
> Thanks.

So something like:

class=“\<%= cycle(‘green’, ‘green’, ‘green’, ‘white’, ‘white’, ‘white’)  
%\>”\>

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [March 11, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/alternate-colors-for-row-blocks/131585/3 "2008-03-11T18:27:09Z")

</div>

Rob B. wrote:

> class="\<%= cycle(‘green’, ‘green’, ‘green’, ‘white’, ‘white’, ‘white’)  
> %\>"\>

Thanks for the suggestion, but I’m not sure this will work in my  
situation.

There is always going to be a first(x) and second(y) row. The third(z)  
row may be populated based on the results of the second row. If “y”  
finds a record, then the “z” row created with a value . If “y” does not  
find a value, then it ends the loop and goes to the next “x” record.

Additionally I have the records split into 3 rows.

The code looks similar to this.

\<% for x in @x.id %\>

 \<%=h x.id %\> \<%=h y.id %\> \<%# if y returns a value %\> \<%=h z.id %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [March 11, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/alternate-colors-for-row-blocks/131585/4 "2008-03-11T18:44:09Z")

</div>

Becca G. wrote:

> \<%=h x.id %\> \<%=h y.id %\> \<%# if y returns a value %\> \<%=h z.id %\> \<% end %\>

Call "cycle’ only once inside each loop, such as…

\<% for x in @x.id %\>  
\<% row\_group\_class = cycle(‘green’,‘white’) %\>

 \>\<%=h x.id %\> \>\<%=h y.id %\> \<%# if y returns a value %\> \>\<%=h z.id %\> \<% end %\>

–  
[http://www.5valleys.com/](http://www.5valleys.com/)  
[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [March 11, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/alternate-colors-for-row-blocks/131585/5 "2008-03-11T19:44:41Z")

</div>

Jon G. wrote:

> Call "cycle’ only once inside each loop, such as…
> 
> \<% for x in @x.id %\>  
> \<% row\_group\_class = cycle(‘green’,‘white’) %\>
> 
> \>\<%=h x.id %\> \>\<%=h y.id %\> \<%# if y returns a value %\> \>\<%=h z.id %\> \<% end %\>

That’s perfect! Thanks!!!
