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.
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"}
By using JavaScript you can easily disable a particular value in drop-down as:
$(/* option selector */).prop('disabled', true);
See it in action
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With