Created
November 23, 2012 02:17
-
-
Save johnantoni/4133732 to your computer and use it in GitHub Desktop.
form-state-select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
needs http://james.padolsey.com/javascript/regex-selector-for-jquery/