Ruby on Rails 下拉列表<选择>菜单 - 如何为选项设置样式或应用类?

发布于 2024-12-10 17:53:58 字数 949 浏览 0 评论 0原文

如何在 ruby​​ on Rails 3 中创建颜色下拉菜单?

我知道如何在 html、css 和 javascript 中执行此操作,如下所示,但我不知道如何在 ruby​​ on Rails 3 中执行此操作。

理想情况下,使用 collection_select 或 collection_options_for_select 构建

我想要得到的部分代码是:

<select>
<option value="">Highlight</option>
<option value="#000000" style="background-color: Black;color: #FFFFFF;">Black</option>
<option value="#808080" style="background-color: Gray;">Gray</option>
<option value="#A9A9A9" style="background-color: DarkGray;">DarkGray</option>

完整代码位于: http://pietschsoft.com/post/2004/09/20/Color-the-background-of-items-in-a-Dropdown-box-in-your-HTML-pages.aspx< /a>

How can I create a colors dropdown menu in ruby on rails 3?

I know how to do it in html, css and javascript as following but i dont know how in ruby on rails 3.

Ideally using collection_select or collection_options_for_select other collection objects that build <select>'s and <options>'s. Using these would be preferred to manually looping through the collection and spitting out pieces and styling each piece though that could be done.

Code partial of what I am trying to get is:

<select>
<option value="">Highlight</option>
<option value="#000000" style="background-color: Black;color: #FFFFFF;">Black</option>
<option value="#808080" style="background-color: Gray;">Gray</option>
<option value="#A9A9A9" style="background-color: DarkGray;">DarkGray</option>

Full Code is at:
http://pietschsoft.com/post/2004/09/20/Color-the-background-of-items-in-a-Dropdown-box-in-your-HTML-pages.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

死开点丶别碍眼 2024-12-17 17:53:58

Rails Helper options_for_select 绝对允许您为各个选项提供样式信息。

这直接来自 options_for_select 的文档

  options_for_select([ "Denmark", ["USA", {:class=>'bold'}], "Sweden" ], ["USA", "Sweden"])

给出

  <option value="Denmark">Denmark</option>\n<option value="USA" class="bold" selected="selected">USA</option>\n<option value="Sweden" selected="selected">Sweden</option>

  options_for_select([["Dollar", "$", {:class=>"bold"}], ["Kroner", "DKK", {:onclick => "alert('HI');"}]])

给出

  <option value="$" class="bold">Dollar</option>\n<option value="DKK" onclick="alert('HI');">Kroner</option>

在你的情况下它将是

options_for_select([["Black", {:style => "background-color: Black; color: #ffffff"}],
                    ["Gray", {:style => "background-color: Gray"}],
                    ["DarkGray", {:style => "backgorund-color: DarkGray"}]])

Rails Helper options_for_select absolutely allows you to provide styling information for individual options.

This is straight from the documentation of options_for_select

  options_for_select([ "Denmark", ["USA", {:class=>'bold'}], "Sweden" ], ["USA", "Sweden"])

gives

  <option value="Denmark">Denmark</option>\n<option value="USA" class="bold" selected="selected">USA</option>\n<option value="Sweden" selected="selected">Sweden</option>

and

  options_for_select([["Dollar", "$", {:class=>"bold"}], ["Kroner", "DKK", {:onclick => "alert('HI');"}]])

gives

  <option value="$" class="bold">Dollar</option>\n<option value="DKK" onclick="alert('HI');">Kroner</option>

And in your case it would be

options_for_select([["Black", {:style => "background-color: Black; color: #ffffff"}],
                    ["Gray", {:style => "background-color: Gray"}],
                    ["DarkGray", {:style => "backgorund-color: DarkGray"}]])
情愿 2024-12-17 17:53:58

据我所知,内置的助手不支持它。
但是,您可以使用以下命令自行推出:

def options_colors(colors)
  colors.collect do |color, code| 
    "<option value='#{code}' style='background-color:#{code};'>#{color}</option> "
  end.join
end

然后按如下方式使用它:

@colors = ["Red" => "#f00", "Blue" => "blue"]
select_tag("setting[bg_color]", options_colors(@colors))

The built in helpers don't support it as far as I know.
However you can roll your own with:

def options_colors(colors)
  colors.collect do |color, code| 
    "<option value='#{code}' style='background-color:#{code};'>#{color}</option> "
  end.join
end

and then use it as follow:

@colors = ["Red" => "#f00", "Blue" => "blue"]
select_tag("setting[bg_color]", options_colors(@colors))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文