0

我有兩種型號Employee,EmployeeDepartment。我做了一個動態的選擇菜單,所以當我選擇一個部門,將通過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)

guidance_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,有沒有什麼辦法可以實現這一點?

+0

請添加動態選擇代碼和完整控制器動作代碼。 – tommasop

+0

它是一個正常的控制器,我添加了什麼只與我的問題有關。 ,還加入了問題 –

回答

0

您需要使用類似Gon的東西。它有一個watch功能,可以幫助重新加載數據,而無需重新加載頁面see here

您的代碼的問題只是它過濾頁面而不重新加載它。所以基本上是在@supervisors_list內置服務器端,您可以:

  1. 使用AJAX方法來獲得新的從服務器過濾@supervisor_list(坤錶,或直接使用jQuery .ajax
  2. @supervisor_list數據添加到您的頁面的JavaScript和過濾IT部門過濾後(也通過坤或read this railscast
+0

的js代碼那麼我的代碼呢?它的工作,但我需要得到特定的員工不是所有的員工 –