开发者

select_tag helper with grouped_options_for_select order

I have this in my model:

LOCATION_IN_UK = {'England' => [['Berkshire', 1],['Cambridgeshire',2],['Cheshire',3]], 'Scotland' => [['Dumfries and Galloway',4],['Fife',5],['Lothian',6]], 'Others' => [['Outside UK',7]]}

And this is in the view:

<%= select_tag :location, grouped_options_for_select(Location::LOCATION_IN_UK), :id => 'location-dropdown' %>

This code generate following html:

<select id="location-dropdown" name="location">
  <optgroup label="England">
    <option value="1">Berkshire</option> 
    <option value="2">Cambridgeshire</option> 
    <option value="3">Cheshire</option></optgroup>
  <optgroup label="Others">
    <option value="7">Outside UK</option></optgroup>
  <optgroup label="Scotland">
    <option value="4">Dumfries and Galloway</option> 
    <option value="5">Fife</option> 
    <option value="6">Lothian</option></optgroup>
</select> 

1. How to skip alphabetical sort order. I want elements locate ex开发者_如何学Goactly as in the hash LOCATION_IN_UK.

2. How to insert prompt into this? :prompt => 'Please select' Doesn't work


To answer your prompt question, prompt is not a hash, it is the third parameter of the method call. So you would do:

 <%= select_tag :location, grouped_options_for_select(LOCATIONS_IN_UK, nil, "Please Select"), :id => 'location-dropdown' %>

And looking at the source code, it seems there is no way to skip the sorting. You could write your own helper method though. Here is the source

# File actionpack/lib/action_view/helpers/form_options_helper.rb, line 449
      def grouped_options_for_select(grouped_options, selected_key = nil, prompt = nil)
        body = ''
        body << content_tag(:option, prompt, { :value => "" }, true) if prompt

        grouped_options = grouped_options.sort if grouped_options.is_a?(Hash)

        grouped_options.each do |group|
          body << content_tag(:optgroup, options_for_select(group[1], selected_key), :label => group[0])
        end

        body.html_safe
      end

You could modify/override that method, but that may break if you are using this function elsewhere, which is why I would suggest you put the following in your application_helper.

def unsorted_grouped_options_for_select(grouped_options, selected_key = nil, prompt = nil)
  body = ''
  body << content_tag(:option, prompt, { :value => "" }, true) if prompt

  ##Remove sort
  #grouped_options = grouped_options.sort if grouped_options.is_a?(Hash)

  grouped_options.each do |group|
    body << content_tag(:optgroup, options_for_select(group[1], selected_key), :label => group[0])
  end

  body.html_safe
end

You can then call unsorted_grouped_options_for_select and it should work.

 <%= select_tag :location, unsorted_grouped_options_for_select(LOCATION::LOCATION_IN_UK, nil, "Please Select"), :id => 'location-dropdown' %>


I had the same problem. You can solve this by using the array version instead of hashes since it only orders it if it is_a?(Hash). See the docs for the format: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/grouped_options_for_select

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜