【问题标题】:Query in Dynamic Select Menu在动态选择菜单中查询
【发布时间】:2013-11-02 07:59:27
【问题描述】:

我有两个模型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' =&gt; 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,有什么方法可以实现吗?

【问题讨论】:

  • 请添加动态选择代码和完整的控制器动作代码。
  • 它是一个普通的控制器,我只添加了与我的问题相关的内容。 ,也是问题中添加的js代码

标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.2 simple-form


【解决方案1】:

您需要使用Gon 之类的内容。它有一个watch 功能,可以帮助重新加载数据而无需重新加载页面see here

您的代码的问题只是它过滤了页面而不重新加载它。所以基本上是 @supervisors_list 构建的服务器端,你可以:

  1. 使用ajax方法从服务器获取新过滤的@supervisor_list(gon watch或者直接使用jQuery.ajax
  2. @supervisor_list数据添加到您的页面javascript并在部门过滤后对其进行过滤(也可以通过gon或read this railscast

【讨论】:

  • 我的代码呢?它的工作,但我需要让特定的员工不是所有的员工
【解决方案2】:

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

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

这可能会帮助您解决问题。

【讨论】:

    猜你喜欢
    • 2015-03-03
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多