开发者

How to make optgroup label selectable in Rails form?

开发者 https://www.devze.com 2023-01-02 20:41 出处:网络
I have created the following select using the option_groups_from_collection_for_select form helper in Rails as part of a search feature.

I have created the following select using the option_groups_from_collection_for_select form helper in Rails as part of a search feature.

<select>
  <option>All Categories</option>
  <optgroup label="Garden">
    <option value="1">Garden Accessories</option>
    <option value="2">Gardens Random</option>
    <option value="3">Sheds</option>
    <option value="4">Playsets</option>
  </opt开发者_JAVA技巧group>
<select> 

The problem is, I want a user to be able to search all of the "Garden" sub-categories at once, but as "Garden" is a label, this does not work, it forces the user to pick one of the sub-categories.

One idea is to somehow be able to add a <option>All Garden<option> tag within the optgroup, but not sure how to inject this into the list.

My search form has no room to add a second sub-category select box.


I figured this out,

In the method I am using to get the child-categories for the optgroup, I just append the current model / category to the array of child categories returned from the AR find call and it works a treat.

.unshift(self.dup)

Hope this helps someone.

0

精彩评论

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