0

我有一个旧版 Rails 3.2.14 应用程序,我试图通过关联、grouped_collection_select 和一些 CoffeeScript 按区域限制设施列表。我最初的问题在这里:CoffeeScript to dynamic change form field on change and load。在一些帮助下,我们最初能够解决问题并实现以下行为。

在调用时选择创建区域,并且设施受其所属区域的约束,此字段称为 transfer_from_id。当使用 Facility 模型中未列出的地址创建呼叫时,我们使用 transfer_from_other 字段来输入地址。发生这种情况时,transfer_from_id 字段故意留空并注入 NIL 以防止 CoffeeScript 自动填充模型中的第一个设施。

编辑 transfer_from_id 包含值的呼叫时, transfer_from_id 会在表单中显示设施名称。

最初,下面的代码在 Chrome 中运行良好,但是当我去测试 Firefox on call create 中的行为时,即使我们在 CoffeeScript 中添加了一个空白选项,它也会使用列表中的第一个工具自动填充 transfer_from_id 字段。编辑呼叫时,会观察到相同的行为。

当我们使用这个应用程序跨平台/浏览器时,我想弄清楚如何在 Firefox 中解决这个问题。我真的很想让这个功能工作,但我不知道为什么它在 Firefox 中不起作用。我已经使用 firebug 尝试对此进行调试,但是我在控制台中看不到任何错误。

以下是我的代码库的摘录,可能有助于说明我正在尝试做的事情:

调用/_form.html.erb 摘录

  <%= f.label :region %>
  <%= f.collection_select(:region_id, Region.all, :id, :area, {:include_blank => true}, {:class => 'select', required: true}) %>
  <%= f.label :caller_name %>
  <%= f.text_field :caller_name, :placeholder => 'Jane Smith', required: true %>
  <%= f.label :caller_phone %>
  <%= f.text_field :caller_phone, :placeholder => 'xxx-xxx-xxxx', required: true %>
  <%= f.label :Transfer_From %>
  <%= f.grouped_collection_select :transfer_from_id, Region.order(:area), :active_facilities, :area, :id, :facility_name, {include_blank: true}, class: 'select' %>
  <%= f.label :Transfer_From_Other%>
  <%= f.text_field :transfer_from_other %>
  <%= f.label :Location_Of_Patient %>
  <%= f.text_field :facility_from_location %>
  <%= f.label :Transfer_To %>
  <%= f.grouped_collection_select :transfer_to_id, Region.order(:area), :active_facilities, :area, :id, :facility_name, {include_blank: true}, class: 'select' %>
  <%= f.label :Transfer_To_Other %>
  <%= f.text_field :transfer_to_other %>

call.js.coffee

jQuery ->
  facilities = $('#call_transfer_from_id').html()

  update_facilities = ->
    region = $('#call_region_id :selected').text()
    options = $(facilities).filter("optgroup[label=#{region}]").html()

    if options
      # Set the options
      $('#call_transfer_from_id').html(options)
      # Add in a blank option at the top
      $('#call_transfer_from_id').prepend("<option value=''></option>")
    else
      $('#call_transfer_from_id').empty()      

  $('#call_region_id').change ->
    update_facilities()

  update_facilities()

设施.rb

belongs_to :region
scope :active, where(status: "Active").order('facility_name ASC')

区域.rb

  has_many :facilities

  def active_facilities 
    self.facilities.active
  end

我对 CoffeeScript 很陌生,我的 JS/jQuery 技能现在还不是很熟练,所以我可以在这方面使用一些帮助来让它跨浏览器工作。非常感谢任何提示或建议。我希望这不是一个重复的问题,如果我的问题不清楚或者您需要更多信息,请随时告诉我。

4

1 回答 1

0

经过一些黑客攻击和试验/错误后,我能够对适用于所有浏览器的 CoffeeScript 进行此编辑。似乎 prepend 方法在最新版本的 Firefox 中无法正常运行,因此我包含了一个空白选项,然后在一行中添加了设施选项数组。

jQuery ->
  facilities = $('#call_transfer_from_id').html()

  update_facilities = ->
    region = $('#call_region_id :selected').text()
    options = $(facilities).filter("optgroup[label=#{region}]").html()

    if options
      # Set the options and include a blank option at the top
      $('#call_transfer_from_id').html("<option value=''></option>" + options)
      # Ensure that the blank option is selected
      $('#call_transfer_from_id').attr("selected", "selected")
    else
      $('#call_transfer_from_id').empty()

  $('#call_region_id').change ->
    update_facilities()

  update_facilities()
于 2014-09-17T15:21:21.770 回答