【问题标题】:How to change background color of collection_select items in Rails?如何更改 Rails 中 collection_select 项目的背景颜色?
【发布时间】:2016-11-01 23:37:37
【问题描述】:

如何在 Rails 中设置集合选择的可视下拉列表的样式?我想更改下拉列表中各个项目的背景颜色,颜色源自 Affiliation 对象中的属性。这是当前的collection_select:

<%= collection_select(:application, :affiliation_id, Affiliation.all.order(:priority).all, :id, :name, :include_blank => true)  %>

Affiliation 对象有一个名为“priority”的属性,我想根据每个 Affiliation 的优先级分配要显示的颜色。

【问题讨论】:

标签: css ruby-on-rails collection-select


【解决方案1】:

添加

class: 'color_<%= affiliation.color %>'

在collection_select中创建相同的样式类

.color_1 { background-color:#colorvalue; }  # for affiliation.color == 1

在css文件中

【讨论】:

    【解决方案2】:

    在你的 .css 文件中为 collection_select 添加额外的类

    .collection_select_background { background: #color_name; }

    您可以使用名为.collection_select_background 的类,即

    &lt;%= collection_select(:application, :affiliation_id, Affiliation.all.order(:priority).all, :id, :name, :include_blank =&gt; true), **class: collection_select_background** %&gt;

    我希望这对你有用。

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多