根据之前的字段选择过滤字段 [英] Filter field based on previous field selection

查看:50
本文介绍了根据之前的字段选择过滤字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 simple_form 根据先前的字段值过滤字段?

例如,我有一个商机表单,其中包含两个字段:公司和联系人.

公司字段:

 

<%= f.association :company, collection: Company.all.order(:account), prompt: "", :label_method =>:account, :value_method =>:id%>

联系人字段:

 

<%= f.association :contact, collection: Contact.all.order(:first_name), prompt: "", :label_method =>拉姆达 { |联系 |"#{contact.first_name} #{contact.last_name}" }, :value_method =>:id%>

这是我想要做的:如果我从上面的公司字段中选择一个名为Deviant"的公司,我希望联系人"字段只显示与名为Deviant"的公司关联的那些联系人.

我正在尝试这样的事情,但无法让它工作:

 

<%= f.association :contact, collection: Contact.where("company_id = ?", params[:id]), prompt: "", :label_method =>拉姆达 { |联系 |"#{contact.first_name} #{contact.last_name}" }, :value_method =>:id%>

我不知道如何引用 Company 字段中的值.

我该怎么做?

谢谢.

更新

有人吗?当然,这一定是可能的.这是任何形式的关键功能.我希望我不需要 jQuery 或其他东西.

解决方案

我认为最好的方法是使用 ajax 请求在公司的选定值发生更改时动态更新您的联系人集合.

首先,您需要在联系人控制器中执行一个操作:

app/controllers/contacts_controller.rb

class ContactsController <应用控制器defcontacts_list如果参数[:company_id]@contacts = Contact.where(company_id: params[:company_id])别的@contacts = Contact.all结尾response_with(@contacts) do |format|format.json { 渲染:json =>@contacts.to_json(:only => [:id, :first_name, :last_name]) }结尾结尾结尾

将此添加到您的路线:

config/routes.rb

 post 'contacts_list' =>contacts#contacts_list",如::contacts_list

然后使用下面的 coffeescript 代码填充您的联系人集合:

app/assets/javasctipts/companies.js.coffee

$(document).ready ->如果 $("#opportunity_company_id")populate_contacts()$("#opportunity_company_id").change ->populate_contacts()populate_contacts = ->$contacts_select = $("select#opportunity_contact_id")$contacts_select.attr已禁用"、已禁用"company_id = $("select#opportunity_company_id").val()如果 company_id 是"$contacts_select.html ""别的$contacts_select.html "<option value=\"\">(正在加载联系人...)</option>"数据 = {company_id: company_id}数据[window._auth_token_name] = window._auth_token$.ajax "/contacts_list",类型:帖子"数据类型:json"数据:数据成功:(联系人)->_html = '<option value="">选择联系人:</option>'_html += '<option value="'+contact.id+'">'+contact.first_name + ' ' + contact.last_name + '</option>'联系人中的联系人$contacts_select.html _html$contacts_select.removeAttr已禁用"错误:->警报尝试加载联系人时出错."

最后,在你的 html 的 head 标签内:

<% ifprotect_against_forgery?%><脚本>window._auth_token_name = "<%= request_forgery_protection_token %>";window._auth_token = "<%= form_authenticity_token %>";<%结束%>

希望能帮到你...

更新:将以下行添加到您的 ApplicationController (app/controllers/application_controller.rb):

respond_to :html, :xml, :json, :js

How can I use simple_form to filter a field, based on a previous fields value?

For instance, I have an Opportunities form, with two fields, Company and Contact.

Company Field:

  <div class="form-group">
    <%= f.association :company, collection: Company.all.order(:account), prompt: "", :label_method => :account, :value_method => :id %>
  </div>

Contact Field:

  <div class="form-group">
    <%= f.association :contact, collection: Contact.all.order(:first_name), prompt: "", :label_method => lambda { |contact| "#{contact.first_name} #{contact.last_name}" }, :value_method => :id %>
  </div>

Here is what I want to do: If I select a company called "Deviant" from the Company field above, I want the Contact field to only display those contacts associated with the company called "Deviant".

I am trying something like this, but can't get it to work:

  <div class="form-group">
    <%= f.association :contact, collection: Contact.where("company_id = ?", params[:id]), prompt: "", :label_method => lambda { |contact| "#{contact.first_name} #{contact.last_name}" }, :value_method => :id %>
  </div>

I don't know how to reference the value in the Company field.

How can I do this?

Thanks.

Update

Anyone? Surely this must be possible. This is a key functionality in any form. I would hope I don't need jQuery or something.

解决方案

I think the best approach is to use ajax requests to update your contacts collection dinamically whenever the company's selected value is changed.

First you'll need an action in your contacts controller:

app/controllers/contacts_controller.rb

class ContactsController < ApplicationController
  def contacts_list
    if params[:company_id]
      @contacts = Contact.where(company_id: params[:company_id])
    else
      @contacts = Contact.all
    end

    respond_with(@contacts) do |format|
      format.json { render :json => @contacts.to_json(:only => [:id, :first_name, :last_name]) }
    end        
  end
end

Add this to your routes:

config/routes.rb

  post 'contacts_list' => "contacts#contacts_list", as: :contacts_list

Then use the coffeescript code bellow to populate your contacts' collection:

app/assets/javasctipts/companies.js.coffee

$(document).ready ->
  if $("#opportunity_company_id")
    populate_contacts()
    $("#opportunity_company_id").change ->
      populate_contacts()

populate_contacts = ->
  $contacts_select = $("select#opportunity_contact_id")
  $contacts_select.attr "disabled", "disabled"
  company_id = $("select#opportunity_company_id").val()
  if company_id is ""
    $contacts_select.html "<option value=\"\">(select the company first)</option>"
  else
    $contacts_select.html "<option value=\"\">(loading contacts...)</option>"
    data = {company_id: company_id}
    data[window._auth_token_name] = window._auth_token
    $.ajax "/contacts_list",
      type: "post"
      dataType: "json"
      data: data
      success: (contacts) ->
        _html = '<option value="">Select the contact:</option>'
        _html += '<option value="'+contact.id+'">'+contact.first_name + ' ' + contact.last_name + '</option>' for contact in contacts
        $contacts_select.html _html
        $contacts_select.removeAttr "disabled"
      error: ->
        alert 'Error trying to load contacts.'

Finally, inside your html's head tag:

<% if protect_against_forgery? %>
  <script>
    window._auth_token_name = "<%= request_forgery_protection_token %>";
    window._auth_token = "<%= form_authenticity_token %>";
  </script>
<% end %>

Hope it helps...

update: Add the following line to your ApplicationController (app/controllers/application_controller.rb):

respond_to :html, :xml, :json, :js

这篇关于根据之前的字段选择过滤字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆