Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups.

#977

Returns a string of <option> tags, using options_for_select, but wraps them with <optgroup> tags.


#Sample usage:
labels = ['North America','Europe']
groups = [['United States','Canada'], ['Denmark','Germany','France']]
optgroups_with_options_for_select(labels,groups)

#Possible output:
<optgroup label="North America">
  <option value="United States">United States</option>
  <option value="Canada">Canada</option>
</optgroup>
<optgroup label="Europe">
  <option value="Denmark">Denmark</option>
  <option value="Germany">Germany</option>
  <option value="France">France</option>
</optgroup>

Reported by Jon · September 5th, 2008 @ 04:28 PM

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Mislav
    Mislav

    +1

    I would like to see better naming:

    
    groups = ['North America','Europe']
    options = [, ]
    grouped_options_for_select(groups, options)
    
    # "option_groups_for_select" is also a good method name
    

    And how about this:

    
    grouped_options_for_select(
      'North America' => ['United States','Canada'],
      'Europe' => ['Denmark','Germany','France']
    )
    # => iterates over keys in alphabetical order ("Europe" would come first)
    

    September 5th, 2008 @ 09:33 PM

  2. Mislav
    Mislav

    I ruined the 2nd line of my example by using cut instead of copy :/

    September 5th, 2008 @ 09:34 PM

  3. Scott Raymond
  4. Jon
    Jon

    I replied to some of Mislav's thoughts in the IRC channel but I'll repeat here.

    I avoided the options argument name because it is often used in many other helpers to mean an option set of arguments that may be passed. And I used the labels argument name because of the attribute in <optgroup label="Europe''>. I could easily budge on the labels one, but I personally think the options should be avoided. Note: options_for_select uses container.

    And to your point about using a hash and iterating in alphabetical order: In my very first implementation of this helper, I needed the groups in a specific order ("Mens, Womens, Children, Other"). Using alphabetical sorting means you lose a good deal of control (although it's much cleaner on first blush).

    September 5th, 2008 @ 10:22 PM

  5. Yaroslav Markin
  6. Eloy Duran
  7. Mislav
    Mislav

    Jon,

    Re: hash argument and alphabetical order---I've only suggested that this could be an alternative to passing 2 arrays. Array arguments should be used when the user is in need of custom ordering.

    September 8th, 2008 @ 01:08 AM

  8. Tom Lea
    Tom Lea

    The two arrays required to be equal length is a bit of a no no for me, how about accepting:

    
    labels = ['North America',]
    groups = [
      ['North America',
        ['United States','Canada']],
      ['Europe',
        ['Denmark','Germany','France']]
    ]
    
    grouped_options_for_select(labels,groups)
    

    This accepts things in order and will keep them in order, ala Jon's requirements.

    Code designed for this should be able to handle a hash too ([[:a, :b]].each{|c| break c} == {:a => :b}.each{|c| break c}), as per Mislav's ideas.

    This also avoids the slightly icky Error: Sizes don't match, as this is now an impossible state to reach.

    There is no basic issue with the idea, it's good, but needs some more work on the method signature, just does not feel railsy enough.

    Any chance of an updated patch Jon?

    September 8th, 2008 @ 09:49 AM

  9. Tom Lea
    Tom Lea

    1's example is wrong... should be

    
    grouped_options = [
      ['North America',
        ['United States','Canada']],
      ['Europe',
        ['Denmark','Germany','France']]
    ]
    
    grouped_options_for_select(grouped_options)
    

    September 8th, 2008 @ 09:53 AM

  10. Jon
    Jon

    Tom-

    I made the changes you suggested. I never liked that error at the beginning, but I didn't want to raise an exception either. Maybe I'm too nice? :-D

    It now accepts either a nested Array or a Hash.

    I also changed the helper name to grouped_options_for_select.

    Let me know if you guys have any other suggestions.

    September 8th, 2008 @ 05:13 PM

  11. Jon
    Jon
    • Title changed from [PATCH] Added optgroups_with_options_for_select helper to form_options_helper for wrapping option tags in optgroups. to [PATCH] Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups.

    Further updated patch including better testing and improved documentation.

    September 9th, 2008 @ 01:52 AM

  12. grosser
    grosser
    • Tag changed from actionpack, enhancement, helper, patch, tested to actionpack, enhancement, helper, patch, tested

    What i am using atm:

    continents = [['Africa',['Norway','NO']]] select_tag('country',option_groups_for_select(continents,'NO'))

    #continentes = [['Africa',['NA','Namibia'],['XX']...],...] def option_groups_for_select(collection,selected = nil)

    collection.inject('') do |options_for_select,group|
      group_label_string = group[0]
      options_for_select = "<optgroup label=\"#{html_escape(group_label_string)}\">"
      options_for_select += options_for_select(group[1..-1],selected)
      options_for_select += '</optgroup>'
    end
    
    

    end

    September 17th, 2008 @ 12:10 PM

  13. grosser
    grosser

    once again(weird formattting)...

    #continentes = [['Africa',['NA','Namibia'],['XX']...],...]
    def option_groups_for_select(collection,selected = nil)
      collection.inject('') do |options_for_select,group|
        group_label_string = group[0]
        options_for_select = "<optgroup label=\"#{html_escape(group_label_string)}\">"
        options_for_select += options_for_select(group[1..-1],selected)
        options_for_select += '</optgroup>'
      end
    end
    
    

    September 17th, 2008 @ 12:12 PM

  14. Sean Cribbs
  15. grosser.michael+lighthouse (at gmail)
    grosser.michael+lighthouse (at gmail)

    There was a small errror in my above post:

    options_for_select = "<optgroup label=\"#{html_escape(group_label_string)}\">"
    
    

    should be

    options_for_select += "<optgroup label=\"#{html_escape(group_label_string)}\">"
    
    

    October 4th, 2008 @ 08:02 AM

  16. Pratik
    Pratik
    • Assigned user set to Pratik
    • Title changed from [PATCH] Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups. to Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups.

    January 5th, 2009 @ 03:51 PM

  17. Pratik
  18. Repository
    Repository
    • State changed from new to resolved

    (from [8761663a68bd7ddd918f78fb3def4697784024f2]) Added grouped_options_for_select helper method for wrapping option tags in optgroups. [#977 Added grouped_options_for_select helper to form_options_helper for wrapping o... state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    January 29th, 2009 @ 06:01 PM