2016-11-01 5 views
0

Wie kann ich das visuelle Drop-down einer Sammlung in Rails auswählen? Ich möchte die Hintergrundfarbe der einzelnen Elemente in der Dropdown-Liste ändern, wobei die Farbe von einem Attribut im Affiliation-Objekt abgeleitet wird. Hier ist die aktuelle collection_select:Wie ändere ich die Hintergrundfarbe von collection_selects in Rails?

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

Die Zugehörigkeit Objekte haben ein Attribut namens „Priorität“ und ich möchte eine Farbe zugewiesen werden basierend auf jeder Zugehörigkeit Priorität angezeigt werden.

+0

Siehe [this] (http://stackoverflow.com/questions/34247055/rails-how-to-add-custom-data-attributes-in-collection-select) oder [dies] (http: // stackoverflow .com/questions/5052889/ruby-on-rails-f-wählen-optionen-mit-kundenspezifischen-attributen) – byakugie

Antwort

0

hinzufügen

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

in collection_select und gleichen Stil classe erstellen

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

in CSS-Datei

0

In Ihrem CSS-Datei für collection_select Extraklasse hinzufügen

.collection_select_background { background: #color_name; }

Und Sie können Klasse .collection_select_background d.h

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

Ich hoffe, dies wird für Sie arbeiten genannt verwenden.

+0

Mehr für Referenz http://apidock.com/rails/ActionView/Helpers/FormBuilder/collection_select –

Verwandte Themen