在我的Rails应用程序上,我有一个非常基本的架构:
在我的活动管理表单页面上,我想把字段放在每个公司中,我首先在下拉列表中选择它所属的行业(这是目前为止的工作),然后再选择另一个字段“子行业”,根据我在前一个领域中选择的行业,只显示我以前在这个行业中分类过的子行业。
例如,古柯将进入工业“饮料”,而我希望该表格能够动态调整,并且只显示在字段"sub“:带有”热饮料“、”冷饮“、”茶“、”苏打水“的下拉列表。
form do |f|
f.inputs "Company" do
f.input :company_industry_id, :label => "Select industry:", :as => :select, :collection => CompanyIndustry.all
f.input :company_subindfustry_id, :label => "Select subindustry:", :as => :select, :collection => CompanySubindustry.all
end显然,到目前为止,我遇到了一个问题,它向我展示了我所有的分行业,而不仅仅是我在上一个领域选择的行业内的分行业。
有人知道我怎么能解决这个问题吗?
发布于 2013-11-11 15:37:42
您需要进行页面刷新以根据所选内容更新数据,或者需要(我的首选) AJAX调用来更新第一个下拉列表之后的另一个下拉列表。
做这件事有一个很棒的铁轨 --那将是一个很好的起点。
发布于 2014-01-16 20:10:17
我选择以一种快速、肮脏、非AJAX的方式来处理这个问题。对我的场景来说效果很好。
注意,辅助选择中不适用的项是禁用的,而不是隐藏的。如果您确实需要隐藏它们,我将克隆和重建select,而不是禁用特定选项。
#...
f.input :user, :input_html => {
##Manipulate Location select based on selected User
:onchange => "
var user = $(this).val();
$('#order_location_id').val(0).find('option').each(function(){
var $option = $(this),
isCorrectUser = ($option.attr('data-user') === user);
$option.prop('disabled',!isCorrectUser);
});
"
}
##Insert necessary Location info into DOM
f.input :location, collection: Location.all.map{ |loc|
[loc.name,loc.id, {"data-user" => loc.user_id}]
}
#...https://stackoverflow.com/questions/19906198
复制相似问题