javascript - Disable the dropdown value which already exist in database -


can guide me solution. example , there dropdown down in form contains animals name. if user select 1 animal that, saved in dropdown. again in selection process, saved data want become disabled. other options able select. solution want.

you can achieve helper method view, or set instance variables in controller here pseudo code going.

def disabled_animals    animal.where(already_selected: true).pluck(:name) end  def selectable_animals   animal.all.pluck(:name) - disabled_animals end 

the idea 2 arrays, 1 without disabled animals , 1 disabled animals , pass select helper method, in view.:

select("post", "animal", selectable_animals, {disabled: disabled_animals})

which produce this:

<select name="post[animal]" id="post_animal">      <option value=""></option>      <option value="joke">joke</option>      <option value="poem">poem</option>      <option disabled="disabled" value="zebra">zebra</option>  </select> 

reference: http://api.rubyonrails.org/classes/actionview/helpers/formoptionshelper.html


Comments

Popular posts from this blog

many to many - Django Rest Framework ManyToMany filter multiple values -

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

Java Entity Manager - JSON reader was expecting a value but found 'db' -