Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable specific value in dropdown select with simple_form?

I am using simple_form in my rails application. I want to disable a particular value in drop-down.

Here is part of the code

= simple_form_for(@organization,url: admin_organization_path) do |f| 
 = f.input :hospital_name, input_html: { class: "form-control"}
 = f.input :parent, collection: @organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}  

I tried using :disabled => @organizations.first but i was failed. Is there any other method to use.Kindly help me. thanks.

like image 249
vjnan369 Avatar asked Feb 03 '16 12:02

vjnan369


2 Answers

Simpleform builder for select box uses value of the each option to compare with value of disabled attribute, so you just simple have to use id of the organization to disable desired option:

= simple_form_for(@organization,url: admin_organization_path) do |f| 
 = f.input :hospital_name, input_html: { class: "form-control"}
 = f.input :parent, collection: @organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}, disabled: @organizations.first.id

If you want to disable several options for selectbox, you can manually build options list inline or using helper and use it as attribute for input:

= f.input :parent,  collection: @organizations.map{|o| [o.id, o.name, {disabled: o.id.in?([1,21,10])}]}, input_html: { class: "form-control", id: "chosen-select-speciality"}
like image 52
BitOfUniverse Avatar answered Sep 20 '22 02:09

BitOfUniverse


By using JavaScript you can easily disable a particular value in drop-down as:

$(/* option selector */).prop('disabled', true);

See it in action

like image 26
Suneel Avatar answered Sep 20 '22 02:09

Suneel