Rails 中的动态选择菜单?

发布于 2024-09-15 10:31:42 字数 1828 浏览 4 评论 0原文

我正在尝试让动态选择菜单在我的 Rails 应用程序中工作。

我有一个名为 kase 的模型,一个名为 person 的模型和一个名为 company 的模型。

当用户创建新的 kase 时,他们会看到一个用于选择人员的选择字段和一个用于选择公司的选择字段。

我试图使其动态化,因此如果他们在第一个选择字段中选择公司 a,则只有公司 a 的员工才会在人员字段中列出。

模型关联如下:

class Kase < ActiveRecord::Base

  belongs_to :company # foreign key: company_id
  belongs_to :person # foreign key in join table
  belongs_to :surveyor,
             :class_name => "Company",
             :foreign_key => "appointedsurveyor_id"
  belongs_to :surveyorperson,
             :class_name => "Person",
             :foreign_key => "surveyorperson_id"

-------------------------------------------------  

class Company < ActiveRecord::Base
  has_many :kases
  has_many :people
  def to_s; companyname; end

-------------------------------------------------

class Person < ActiveRecord::Base
  has_many :kases # foreign key in join table
  belongs_to :company

更新的 JAVASCRIPT

var people = new Array();
<% for person in @people -%>
  people.push(new Array(<%= person.company_id %>, '<%=h person.personname %>', <%= person.id %>));
<% end -%>

function personSelected() {
  alert('hello world');
  appointedsurveyor_id = $('kase_appointedsurveyor_id').getValue();
  options = $('kase_surveyorperson_id').options;
  options.length = 1;
  people.each(function(person) {
    if (person[0] == appointedsurveyor_id) {
      options[options.length] = new Option(person[0], person[1]);
      alert('hello world')
    }
  });
  if (options.length == 1) {
    $('kase_surveyorperson_id').hide();
  } else {
    $('kase_surveyorperson_id').show();
  }
}

document.observe('dom:loaded', function() {
  $('kase_appointedsurveyor_id').observe('change', personSelected);
});

I am trying to get dynamic select menu's working in my Rails application.

I have a model called kase, a model called person and a model called company.

When a user creates a new kase, they are presented with a select field to choose a Person and a select field to choose a Company.

I am trying to make it dynamic, so if they choose company a in the first select field then only employees of company a will be listed in the Person field.

The model associations are as follows:

class Kase < ActiveRecord::Base

  belongs_to :company # foreign key: company_id
  belongs_to :person # foreign key in join table
  belongs_to :surveyor,
             :class_name => "Company",
             :foreign_key => "appointedsurveyor_id"
  belongs_to :surveyorperson,
             :class_name => "Person",
             :foreign_key => "surveyorperson_id"

-------------------------------------------------  

class Company < ActiveRecord::Base
  has_many :kases
  has_many :people
  def to_s; companyname; end

-------------------------------------------------

class Person < ActiveRecord::Base
  has_many :kases # foreign key in join table
  belongs_to :company

UPDATED JAVASCRIPT

var people = new Array();
<% for person in @people -%>
  people.push(new Array(<%= person.company_id %>, '<%=h person.personname %>', <%= person.id %>));
<% end -%>

function personSelected() {
  alert('hello world');
  appointedsurveyor_id = $('kase_appointedsurveyor_id').getValue();
  options = $('kase_surveyorperson_id').options;
  options.length = 1;
  people.each(function(person) {
    if (person[0] == appointedsurveyor_id) {
      options[options.length] = new Option(person[0], person[1]);
      alert('hello world')
    }
  });
  if (options.length == 1) {
    $('kase_surveyorperson_id').hide();
  } else {
    $('kase_surveyorperson_id').show();
  }
}

document.observe('dom:loaded', function() {
  $('kase_appointedsurveyor_id').observe('change', personSelected);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落在眉间の轻吻 2024-09-22 10:31:42
var people = new Array();
<% for person in @people -%>
  people.push(new Array(<%= person.id %>, '<%=h person.login %>'));
<% end -%>

function personSelected() {
  alert('hello world');
  appointedsurveyor_id = $('company_appointedsurveyor_id').getValue();
  options = $('person_appointedsurveyorperson_id').options;
  options.length = 1;
  people.each(function(person) {
    if (person[0] == appointedsurveyor_id) {
      options[options.length] = new Option(person[0], person[1]);
    }
  });
  if (options.length == 1) {
    $('person_field').hide();
  } else {
    $('person_field').show();
  }
}

document.observe('dom:loaded', function() {
  //companySelected(); << remove this
  $('person_field').observe('change', personSelected);
});
var people = new Array();
<% for person in @people -%>
  people.push(new Array(<%= person.id %>, '<%=h person.login %>'));
<% end -%>

function personSelected() {
  alert('hello world');
  appointedsurveyor_id = $('company_appointedsurveyor_id').getValue();
  options = $('person_appointedsurveyorperson_id').options;
  options.length = 1;
  people.each(function(person) {
    if (person[0] == appointedsurveyor_id) {
      options[options.length] = new Option(person[0], person[1]);
    }
  });
  if (options.length == 1) {
    $('person_field').hide();
  } else {
    $('person_field').show();
  }
}

document.observe('dom:loaded', function() {
  //companySelected(); << remove this
  $('person_field').observe('change', personSelected);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文