Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Chris http://scharfie.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Yet another alternating row implementation

Most of the alternating row implementations I've seen use two css classes, "even" and "odd". However, by simply provide *one* of these, the same functionality can be achieved, and with less code.

In my application_helper.rb, I added the following:

   1  
   2    def even_odd(tag_name, options = {}, open = true)
   3      @even_odd_counters = Hash.new(false) unless defined?(@even_odd_counters)
   4      @even_odd_counters[tag_name] = !@even_odd_counters[tag_name]
   5      ((options['class'] ||= '') << ' odd').strip! if @even_odd_counters[tag_name]
   6      tag(tag_name, options, open)
   7    end
   8  
   9    def even_odd_reset(tag_name)
  10      @even_odd_counters = Hash.new(false) unless defined?(@even_odd_counters)
  11      @even_odd_counters[tag_name] = false
  12    end


This implementation uses the tag() method in Rails, so you simply pass it the same arguments. One difference, however, is that the "open" parameter is true by default, as the majority of the time this will be used for TR's (and I use it for DT and DD sometimes).

I have included a simple "reset" method as well. I realize I'm duplicating the first line in both methods, but this was a quick-and-dirty addition.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS