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
-=-
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2006-Oct-17  16:15 UTC
Re: How to add odd_or_even automatically in the for loop
> 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.cycle() might be what you''re looking for... http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000518 <%- for item in @items do -%> <tr class="<%= cycle("even", "odd") %>"> ... use item ... </tr> <%- end -%> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jason Frankovitz
2006-Oct-17  16:37 UTC
Re: How to add odd_or_even automatically in the for loop
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
On Oct 17, 2006, at 8:47 AM, Neeraj Kumar wrote:
> 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
> -=-
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Chris Gernon
2006-Oct-17  16:49 UTC
Re: How to add odd_or_even automatically in the for loop
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 %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Neeraj Kumar
2006-Oct-17  16:54 UTC
Re: How to add odd_or_even automatically in the for loop
Thanks. Both the suggestions are neat. -=- On 10/17/06, Chris Gernon <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 %> > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Chris Gernon
2006-Oct-17  17:43 UTC
Re: How to add odd_or_even automatically in the for loop
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.
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---