Sweetest Way To Created Alternate Colored Rows

There are many ways to create altnerate colors rows in a table but I
was wondering if their is a nice Rails or Ruby way of doing it? Any
suggestions? :slight_smile:

Your Friend,

John


John K.
[email protected]

http://www.kopanas.com

http://www.soen.info

This is the way I did it:

<% counter = 0 %>
<% @companies.each do |company| -%>
	<li class="<%= ((counter % 2) == 0) ? 'light' : 'dark' %>">
		<% counter += 1 %>

I am sure their is a better way though. I hope :slight_smile:

On 11/22/06, John K. [email protected] wrote:

[email protected]

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info


John K.
[email protected]

http://www.kopanas.com

http://www.soen.info

look into the ‘cycle’ method. you can cycle through a couple different
colors or CSS styles. for example, in your partial or in your output
loop
for the table:

<% css_class = cycle(‘report_detail_row’, ‘report_detail_row_alt’) -%>

and then put that in your table/div output
ed

On 11/22/06, John K. [email protected] wrote:


John K.
[email protected]

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info


Ed Hickey
Developer
Litmus Media
816-533-0409
[email protected]
A Member of Think Partnership, Inc
www.ThinkPartnership.com
Amex ticker symbol: THK

or you could write:
<% @companies.each do |company| -%>

  • –jake

    Ed Hickey wrote:

    look into the ‘cycle’ method. you can cycle through a couple different
    colors or CSS styles. for example, in your partial or in your output
    loop

    for the table:

    <% css_class = cycle(‘report_detail_row’, ‘report_detail_row_alt’) -%>

    and then put that in your table/div output
    ed

    On 11/22/06, John K. [email protected] wrote:


    John K.
    [email protected]

    http://www.kopanas.com
    http://www.cusec.net
    http://www.soen.info


    Ed Hickey
    Developer
    Litmus Media
    816-533-0409
    [email protected]
    A Member of Think Partnership, Inc
    www.ThinkPartnership.com
    Amex ticker symbol: THK

  • sorry, i think you need to just give it the list of strings and not an
    Array. So the following would be correct:

  • –jake

    jake wrote:

    or you could write:
    <% @companies.each do |company| -%>

  • –jake

    Ed Hickey wrote:

    look into the ‘cycle’ method. you can cycle through a couple different
    colors or CSS styles. for example, in your partial or in your output
    loop

    for the table:

    <% css_class = cycle(‘report_detail_row’, ‘report_detail_row_alt’) -%>

    and then put that in your table/div output
    ed

    On 11/22/06, John K. [email protected] wrote:


    John K.
    [email protected]

    http://www.kopanas.com
    http://www.cusec.net
    http://www.soen.info

  • John K. wrote:

    This is the way I did it:

    <% counter = 0 %>
    <% @companies.each do |company| -%>


  • <% counter += 1 %>

    I am sure their is a better way though. I hope :slight_smile:

  • http://rubyonrails.org/api/classes/ActionView/Helpers/TextHelper.html#M000518

    <% @companies.each do |company| -%>
    <li class=“<%= cycle(“light”, “dark”) %>”>

    Alan

    jake wrote:

  • Holy crap, that is awesome.

    I love stumbling onto these happy little nuggets that show how I have
    been doing things the hard way for a long time.

    cycle()

    <% counter = 0 -%>
    <% @companies.each do |company| -%>

  • <% counter = 1 - counter -%>

    On Wed, Nov 22, 2006 at 09:38:29AM -0500, John K. wrote:

    John K.
    [email protected]

    http://www.kopanas.com
    http://www.cusec.net
    http://www.soen.info


    John O.
    Systems Administrator
    Yo!Africa
    E-Mail: john at yoafrica.com
    Tel: +263 4 858404
    “The original point and click interface, was a Smith and Wesson.”

  • I usually do this with JS, since that way I don’t have to add extra
    classes
    to each row.

    addEvent(window, “load”, zebratables_init);

    function zebratables_init() {
    var even = false;
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName(“table”);
    for (ti=0;ti<tbls.length;ti++) {
    thisTbl = tbls[ti];
    if (((’ ‘+thisTbl.className+’ ').indexOf(“zebra”) != -1) &&
    (thisTbl.id)) {
    for (j=1;j<thisTbl.rows.length;j++) {
    thisTbl.rows[j].className =
    even ? ‘tr_even’ : ‘tr_odd’;
    // flip from odd to even, or vice-versa
    even = ! even;
    }
    }
    }
    }

    2006/11/28, John O. [email protected]:

    I don’t like cycle because I want NO class in the non-even rows. Why
    make it harder for the browser to render your table?

    <% @registrations.each_with_index do |registration, i| %>
    <% row_class = i%2 == 1 ? ‘’ : ’ class=“even”’ %>
    <tr<%= row_class %>>
    […]

    <% end %>

    John O. wrote:

    <% counter = 0 -%>
    <% @companies.each do |company| -%>

    Untested warning:

    <% @companies.each_with_index do |company, idx| -%>

  • ...


    Phlip
    Redirecting... ← NOT a blog!!!

  • I also do this with Javascript instead.

    I don’t like cycle because I want NO class in the non-even rows. Why
    make it harder for the browser to render your table?

    <% @registrations.each_with_index do |registration, i| %>
    <% row_class = i%2 == 1 ? ‘’ : ’ class=“even”’ %>
    <tr<%= row_class %>>
    […]

    <% end %>

    Now let’s try the same with cycle:

    <% @registrations.each do |registration| %>

    > [...] <% end %>

    Regards,
    Rimantas

    http://rimantas.com/