Forum: Ruby on Rails How to add odd_or_even automatically in the for loop

934f858e451cf9b771996b2940cd696b?d=identicon&s=25 Raj Singh (dorelal)
on 2006-10-17 17:55
(Received via mailing list)
In every for loop in my application I do something like this

        <%  odd_or_even = 0
            for event in @events
                odd_or_even = 1 - odd_or_even
         %>


I know there are some ways around to that by having automatic javascript
and
other solutions. But we support a large number of browsers and can't be
sure
everyone has the latest browser so we have settled with this solution.

I was wondering if  there is  a way for me to open the for loop
functionaliyty and add this feature

1) Every time the for loop starts initialize odd_or_even to zero.
2) Change the value everytime the loop is executed.
3) Make this varialbe odd_or_even visible exactly the way event is
visible.

Any suggestions.

Thanks
-=-
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-10-17 18:13
(Received via mailing list)
> everyone has the latest browser so we have settled with this solution.
>
> I was wondering if  there is  a way for me to open the for loop
> functionaliyty and add this feature
>
> 1) Every time the for loop starts initialize odd_or_even to zero.
> 2) Change the value everytime the loop is executed.
> 3) Make this varialbe odd_or_even visible exactly the way event is visible.

cycle() might be what you're looking for...

http://api.rubyonrails.org/classes/ActionView/Help...

   <%- for item in @items do -%>
     <tr class="<%= cycle("even", "odd") %>">
       ... use item ...
     </tr>
   <%- end -%>
19fe735490c8f7e08d5c2de7dc25c826?d=identicon&s=25 Jason Frankovitz (Guest)
on 2006-10-17 18:39
(Received via mailing list)
Not sure if this applies to your problem, but have you looked at cycle
()? I use it to alternate row colors in tables:

<% [1,2,3,4].each do |a| %>
<% CSSclassName = cycle("even", "odd") %>
<tr>
<td class='<%= CSSclassName %>'><%= a %></td>
</tr
<% end %>

-Jason

--
Jason Frankovitz - jfrankov at pobox dot com
main 310-601-8454
cell 415-254-4890
De271a04fe7a67b884ce75404c1dcc61?d=identicon&s=25 Chris Gernon (kabigon)
on 2006-10-17 18:49
Not entirely clear on what you're looking for, so I'm not sure if this
will work for you, but I use odd? to generate alternating rows:

<% @records.each_with_index do |record, i| %>
	<tr class="<%= i.odd? ? 'odd' : 'even' %>">
		<td><%=h record.field_1 %></td>
		<td>etc.</td>
	</tr>
<% end %>
934f858e451cf9b771996b2940cd696b?d=identicon&s=25 Raj Singh (dorelal)
on 2006-10-17 18:59
(Received via mailing list)
Thanks.

Both the suggestions are neat.

-=-
De271a04fe7a67b884ce75404c1dcc61?d=identicon&s=25 Chris Gernon (kabigon)
on 2006-10-17 19:43
I think I just figured out a more elegant way of doing this.

class Array
  def each_with_index_parity
    self.each_with_index do |entry, index|
      yield(entry, index, index % 2 == 0 ? 0 : 1)
    end
  end
end

This lets you write:

<% @records.each_with_index_parity do |record, i, parity| %>
	<tr class="<%= ['even', 'odd'][parity] %>">
		<td><%=h record.field_1 %></td>
		<td>etc.</td>
	</tr>
<% end %>

Or, if you prefer, you could put the class names right in the iterator:

class Array
  def each_with_index_parity
    self.each_with_index do |entry, index|
      yield(entry, index, index % 2 == 0 ? 'even' : 'odd')
    end
  end
end

Which gives you:

<% @records.each_with_index_parity do |record, i, parity| %>
	<tr class="<%= parity %>">
		<td><%=h record.field_1 %></td>
		<td>etc.</td>
	</tr>
<% end %>

Just a matter of taste, I guess.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.