<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: alternate code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 08 Sep 2008 04:20:25 GMT</pubDate>
    <description>DZone Snippets: alternate code</description>
    <item>
      <title>classify() for alternating rows, columns, etc.</title>
      <link>http://snippets.dzone.com/posts/show/2569</link>
      <description>I often want different rows in a table to alternate in color, and I do this by assigning each row a class name and styling it with CSS.  This is a simple helper method designed to return a class name based on the given row count.&lt;br /&gt;&lt;br /&gt;It is also convenient for me to assign a class to table cells based on the type of content they hold.  For example, if I have a cell with a float value in it, I want to display it with a monospace font, whereas if I have a cell with a string in it, I want to display it in a serif font.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;# Determines the CSS class based on either the count given&lt;br /&gt;# (returns 'even' or 'odd' as the CSS class name) or the class&lt;br /&gt;# given (returns the string version of the class, lowercased,&lt;br /&gt;# as the CSS class name)&lt;br /&gt;def classify( count_or_class, include_class_text = true )&lt;br /&gt;  if count_or_class.class == Fixnum&lt;br /&gt;    value = ( count_or_class % 2 == 0 ? 'even' : 'odd' )&lt;br /&gt;  else&lt;br /&gt;    value = count_or_class.to_s.downcase&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  if include_class_text&lt;br /&gt;    'class="' &lt;&lt; value &lt;&lt; '"'&lt;br /&gt;  else&lt;br /&gt;    value&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example usage with alternating 'even'/'odd' row class names:&lt;br /&gt;&lt;code&gt;&lt;table&gt;&lt;br /&gt;&lt;% count = 0 %&gt;&lt;br /&gt;&lt;% @collection.each do |value| %&gt;&lt;br /&gt;  &lt;tr &lt;%= classify( count ) %&gt;&gt;&lt;br /&gt;    &lt;td&gt;&lt;%=h value %&gt;&lt;/td&gt;&lt;br /&gt;  &lt;/tr&gt;&lt;br /&gt;  &lt;% count += 1 %&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/table&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example usage with data type class names:&lt;br /&gt;&lt;code&gt;&lt;tr&gt;&lt;br /&gt;&lt;% @columns.each do |column| %&gt;&lt;br /&gt;  &lt;% data = row.send( column.name ) %&gt;&lt;br /&gt;  &lt;td &lt;%= classify( data.class ) %&gt;&gt;&lt;br /&gt;    &lt;%=h data %&gt;&lt;br /&gt;  &lt;/td&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/tr&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Sep 2006 21:00:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2569</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>Alternating table row colours the Rails way</title>
      <link>http://snippets.dzone.com/posts/show/924</link>
      <description>There are several ways of doing this but it's best to use functionality built into Rails:&lt;br /&gt;&lt;br /&gt;  &lt;tr class="&lt;%= cycle("even", "odd") %&gt;"&gt;&lt;br /&gt;    ... use item ...&lt;br /&gt;  &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;API reference:&lt;br /&gt;http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000423</description>
      <pubDate>Thu, 01 Dec 2005 05:38:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/924</guid>
      <author>hammed (Hammed)</author>
    </item>
  </channel>
</rss>
