RAILS AJAX

方法1.用observe_field(观察者,监测select的事件):
1)在view中:
点击触发的select:
<%= collection_select(:person, :area_id, @areas,:id, :name,
options = {}, {:id => 'person_area'})%>
生成观察者的JS(:update是使用了innerHTML,在IE中select.innerHTML不能正确显示其中的option,,解决方法是返回完整的<select>更新其上级元素的innerHTML):
<%= observe_field 'person_area', :update=> 'person_area_places', :url =>{:action => :area_places_options},:with=>'id' %>
二级下拉菜单(需动态更新的):
<%= collection_select(:person, :place_id, @places,:id, :name, { :include_blank => true }, {:id=> 'person_area_places'} ) %>
2)在controller中:
    defarea_places_options
     @places = Place.find_all_by_area_id(params[:id])
     #因为只返回固定的option列,所以干脆连view都省了
     # [Place.new].concat(@places) 是为了能选择空值(对应:include_blank => true)
     render :inline => %{<%=options_from_collection_for_select([Place.new].concat(@places),:id, :name) %>}
   end


方法2.把触发的事件定义直接嵌入到select中:
重点是remote_function的灵活运用
1)在view中:
点击触发的select:
<%= collection_select(:person, :area_id, @areas,:id, :name, options = {}, html_options = {:onChange=> remote_function(:update =>"person_area_places", :url => { :action=> "area_places_options"}, :with =>"'id='+value" )} ) %>

二级下拉菜单(一样):
<%= collection_select(:person, :place_id, @places,:id, :name, { :include_blank => true }, {:id=> 'person_area_places'} ) %>
2)在controller中,与方法1一样


最终采用方案
1)在view中:
点击触发的select:
<%= collection_select(:person, :area_id, @areas,:id, :name, options = {}, html_options = {:onChange=> remote_function(:update =>"person_area_places_box", :url => { :action=> :area_places_select}, :with =>"'id='+value" )} ) %>

二级下拉菜单(为了兼容IE,用个容器包起来):
<spanid="person_area_places_box">
<%= render :partial => 'places'%>
</span>

_places.rhtml:
<%= collection_select(:person, :place_id, @places,:id, :name, { :include_blank => true } )%>

2)在controller中:
    defarea_places_select
     @places = Place.find_all_by_area_id(params[:id])
     render :partial => 'places'
   end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值