Jquery Dropdown搜索导轨2

时间:2012-09-04 12:18:41

标签: jquery ruby-on-rails

如何进行选择(下拉)搜索。我有3个选择,当用户选择下拉列表时,结果div将重新加载以显示主题的结果。

Senario:

     select year | select semester | select course

    <div id="result">
     list of subjects base on the 3 selections
    </div>

2 个答案:

答案 0 :(得分:2)

我不确定下面的代码是否在rails 2.3上运行,但它适用于rails 3

$("#third_select_box_id").change ->
  first_select_box_data = $("#first_select_box_id").val()
  second_select_box_data = $("#second_select_box_id").val()
  third_select_box_data = $("#third_select_box_id").val()
  #add code to check that the first n second dropdown list have some data that has been    selected
  if (the data has been selected in all dropdown lists then )
    $.ajax
      type: "GET"
      data: "year="+first_select_box_data+"&semester="+second_select_box_data+"&course="+third_select_box_data
      url:"/courses/get_results"

如果您想要显示数据,即使未选择年份或学期,也不要在上面的coffeescript中进行检查。在路线

resources: courses do
  get 'get_results'
end
课程控制器中的

def get_results
  @results = #ur logic to collect all results based on received params. Handle the condition that year or semester isnt selected(if u want to)
  respond_to do |format|
    format.js{}
  end
end
get_results.js.haml

中的

$("#result").html("#{escape_javascript(render :partial => 'results')}")
_results.html.haml

中的

#ur logic to display the @results

请考虑使用数据表进行列表,因为它会提高性能。

答案 1 :(得分:0)

发送ajax并收集搜索结果。在search.js.haml中,将div'result'替换为将列出搜索结果的partial。