我有一个三层级联多选菜单。等级是州,城市和位置。选择州后,城市用户可以选择多个位置,例如用户选择1235 Ok Drive
和5678 Drive dr
的位置:
之后,用户可以选择选择其他城市和州,以及其他位置,在这种情况下1 at&t way
:
但是,当用户随后保存时,活动记录不会保留以前选择的1235 Ok Drive
和5678 Drive dr
,而只保留最近的选择,在这种情况下1 at&t way
< / p>
构建菜单:
State:
<%= f.collection_select :id, State.order(:state), :id, :state %>
City:
<%= f.grouped_collection_select :city_ids, State.order(:state), :cities, :state, :id, :city_whole %>
Location:
<%= f.grouped_collection_select(:location_ids, City.order(:city), :locations, :city_whole, :id, :address,{}, {multiple: true}) %>
和CoffeeScript:
jQuery ->
# For the State to City dynamic menu
cities = $('#campaign_city_ids').html()
console.log(cities)
$('#campaign_id').change ->
state = $('#campaign_id :selected').text()
options = $(cities).filter("optgroup[label='#{state}']").html()
console.log(options)
if options
$('#campaign_city_ids').html(options);
else
$('#campaign_city_ids').empty();
# For the City to lcoation dynamic menu
locations = $('#campaign_location_ids').html()
console.log(locations)
$('#campaign_city_ids').change ->
city = $('#campaign_city_ids :selected').text()
city_option = $(locations).filter("optgroup[label='#{city}']").html()
console.log(city_option)
if city_option
$('#campaign_location_ids').html(city_option).trigger('chosen:updated').chosen();
else
$('#campaign_location_ids').empty();
即使用户选择其他选项,如何保持所有选择并保持选定状态?