Ruby-on-Rails:根据先验选择填充选择框 [英] Ruby-on-Rails: populate select box based off prior select
问题描述
我有一组 3 个 collection_select,我想过滤下一个选择.在第一个选择框中选择一个项目时,它应该限制第二个和第三个选择框的可能值.
类似的数据模型如下...
表格和字段
- 锁
- lock_id
- brand_id
- master_id
- regular_id
- 锁品牌
- brand_id
- 万能钥匙
- master_id
- brand_id
- 普通密钥
- regular_id
- brand_id
因此,在填写添加新锁的表单时,会有品牌、主和常规的选择框.选择品牌后,它会将主密钥和常规密钥限制为仅属于所选品牌的密钥.
我找到了一些关于如何执行此操作的部分示例,但没有一个是完整的.我很感激如何实现这一点的完整示例.
您需要做的就是将选择与事件侦听器绑定,该侦听器触发 ajax 请求,填充包含后续选择的其他 2 个部分.即
<%= form_for @thing, :remote =>真的做,:更新=>'second_form' |f|%><%= hidden_field_tag 'form_partial', :value =>'form_2' %><%= select_tag :some_param, [[a,a],[b,b],[c,c]] :class =>'可提交'%><%= f.label :checked %><%=thing.name %><%结束%><div id='second_form'>
<div id='third_form'>
类 ThingController <应用控制器定义更新checkboxes = some_logic_to_select_checkboxes from params[:some_params]渲染:部分=>params[:form_partial], :locals =>{:select_menu =>选择菜单}结尾结尾$('.submittable').live('change', function() {$(this).parents('form:first').submit();返回假;});
您的表单部分应包含填充了您想要启用的任何选择的表单.只是重复表格的想法是多余的.
I have a set of 3 collection_selects that I would like to filter the next select. Upon selecting an item in the first select box, it should limit the possible values of the second and third select boxes.
A similar data model would be as follows...
Tables, and fields
- Lock
- lock_id
- brand_id
- master_id
- regular_id
- Lock Brand
- brand_id
- Master Key
- master_id
- brand_id
- Regular Key
- regular_id
- brand_id
So while filling in the form for adding a new Lock there would be select boxes for brand, master, and regular. Upon selecting a brand, it would then limit the master and regular keys to those that are only of the selected brand.
I've found a couple partial examples of how to do this, however none are complete. I'd appreciate a complete example of how to implement this.
All you need to do is bind the selects with an event listener that fires off an ajax request populating the other 2 partials containing the subsequent selects. i.e.
<%= form_for @thing, :remote => true do, :update => 'second_form' |f| %>
<%= hidden_field_tag 'form_partial', :value => 'form_2' %>
<%= select_tag :some_param, [[a,a],[b,b],[c,c]] :class => 'submittable' %>
<%= f.label :checked %>
<%= thing.name %>
<% end %>
<div id='second_form'>
</div>
<div id='third_form'>
</div>
class ThingController < ApplicationController
def update
checkboxes = some_logic_to_select_checkboxes from params[:some_params]
render :partial => params[:form_partial], :locals => {:select_menu => select_menu}
end
end
$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
return false;
});
Your form partial should contain your forms populated with whatever selects you want enabled. Thought is was redundant to just repeat the form.
这篇关于Ruby-on-Rails:根据先验选择填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!