0

我有两个模型EmployeeEmployeeDepartment。我制作了一个动态选择菜单,因此当我选择一个部门时,它将使用以下代码根据该部门过滤员工simple_form

<%= f.input :employee_id , collection: @departments, as: :grouped_select, group_method: :employees, include_blank: false %>

笔记:@departments = EmployeeDepartment.order(:name)

我想做的是:

在进行动态选择之前,我使用此代码来获取特定员工

<%= f.association :employee, :collection => @supervisors_list, :label_method => :full_name, :value_method => :id, include_blank: false  %>

注意:@supervisors_list = Employee.where('guidance_supervisor' => true).order(:first_name)

guide_supervisors.js.coffee

jQuery ->
  $('#guidance_supervisor_employee_id').parent().hide()
  employee = $('#guidance_supervisor_employee_id').html()
  $('#guidance_supervisor_employee_department_id').change ->
    department = $('#guidance_supervisor_employee_department_id :selected').text()
    escaped_department = department.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(employee).filter("optgroup[label='#{escaped_department}']").html()
    if options
      $('#guidance_supervisor_employee_id').html(options)
      $('#guidance_supervisor_employee_id').parent().show()
    else
      $('#guidance_supervisor_employee_id').empty()
      $('#guidance_supervisor_employee_id').parent().hide()

添加动态选择后,我无法使用@supervisors_list这使得菜单获取所有员工。我需要能够@supervisors_list再次使用,有什么方法可以实现吗?

4

2 回答 2

0

You need to use something like Gon. It has a watch feature that can help reload data without reloading page see here

Te problem with your code is only that it filters the page without reloading it. So basically being the @supervisors_list built server side you can:

  1. use an ajax method to get the new filtered @supervisor_list from the server (gon watch or directly use jQuery .ajax)
  2. add the @supervisor_list data to your page javascript and filter it after department filtering (also through gon or read this railscast)
于 2013-11-02T08:34:27.360 回答
0

有一个简单的方法来实现它。

https://gist.github.com/romansklenar/3077281

这可以帮助您解决问题。

于 2013-11-02T09:58:55.640 回答