Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from johnantoni/state-select.js.coffee
Last active August 29, 2015 14:16
Show Gist options
  • Save kivanio/15a51934880b03e08b14 to your computer and use it in GitHub Desktop.
Save kivanio/15a51934880b03e08b14 to your computer and use it in GitHub Desktop.
simple-form
-----------
= f.input :country_id, :as => :select, :collection => Country.priority, include_blank: false, :label => "Country :", :priority => [ "United States", "Canada" ]
= f.input :state_id, :label => "State :" do
= f.grouped_collection_select :state_id, Country.order(:name), :states, :name, :id, :name, include_blank: false
@formStateSelect = (form_name) ->
$("form:regex(id, .*#{form_name}.*)").each (index, element) =>
id = "##{$(element).attr("id")}"
console.log id
stateSelect("#{id} ##{form_name}_country_id", "#{id} ##{form_name}_state_id")
@stateSelect = (country_id, state_id) ->
country_el = $(country_id)
state_el = $(state_id)
console.log "#{country_id} #{state_id}"
if state_el
#state_el.parent().hide()
unless (country_el.val() == '1228')
state_el.parents('.control-group').hide()
states = state_el.html()
console.log country_el.val()
country_el.change ->
country = $("#{country_id} :selected").text()
escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(states).filter("optgroup[label=#{escaped_country}]").html()
if options
state_el.html(options)
#state_el.parent().show()
state_el.parents('.control-group').show()
else
state_el.empty()
#state_el.parent().hide()
state_el.parents('.control-group').hide()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment