【问题标题】:Is there a way to string these together so they work together at the same time basically?有没有办法将它们串在一起,这样它们基本上可以同时工作?
【发布时间】:2015-03-25 11:38:46
【问题描述】:

在我的 Rails application.js 中,我有两个 javascript,它们的作用是自动填充我的注册表单。第一个基于将放入我的 emp_id text_field 的内容,一旦将某些内容放入其中,并且它与我在另一个表中的内容匹配,它将提取用户的名字和 person_id 并将它们放置在适当的 text_fields 中。现在我有第二个 javascript,它查看 person_id text_field 中的数据,因为现在从第一个 javascript 中它有数据但是,让第二个工作的唯一方法是如果我更改一个数字并点击选项卡,那么它将填写我的 manager_first_name text_field ...有没有办法让第一个 javascript 提取初始数据,然后第二个自动对数据做出反应,而无需我点击它或更改 person_id text_field?

这是我的用户控制器,这些是我的 javascipts 中的方法。

类用户控制器

 def populate_form
  @visual = Visual.find_by_id(params[:emp_id])
  @emp_first_name = @visual.first_name
  @person_id = @visual.pds_alias

    render :json => {

        :emp_first_name => @emp_first_name,
        :person_id => @person_id

    }

  end

  def populate_manager
    @manager = Manager.find_by_person_id(params[:person_id])
    @mgr_first_name = @manager.mgr_name

     render :json => {

        :mgr_first_name => @mgr_first_name
     }

   end
 end

这是我的看法

<div class='row form-group'>
   <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
    <%= f.text_field :emp_id, tabindex: 1, id: 'emp_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
  </div>
</div>

 <div class='row form-group'>
  <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
    <%= f.text_field :emp_first_name, tabindex: 1, id: 'emp_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
   </div>
 </div>



 <div class='row form-group'>
  <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
    <%= f.text_field :person_id, tabindex: 1, id: 'person_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
   </div>
 </div>



 <div class='row form-group'>
  <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
    <%= f.text_field :mgr_first_name, tabindex: 1, id: 'mgr_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
   </div>
 </div>

这是我的 App.js

 $(document).ready(function(){

   $('#emp_id').change(function() {
         var url = "/user/populate_form?emp_id="+$(this).val();
         $.getJSON(url, function(data) {
           if(!(data.emp_first_name === undefined))
           $('#emp_first_name').val(data.emp_first_name);
           if(!(data.person_id === undefined))
           $('#person_id').val(data.person_id);
         });
       }
     );
   });

$(document).ready(function(){

    $('#person_id').click(function() {
         var url = "/user/populate_manager?person_id="+$(this).val();
         $.getJSON(url, function(data) {
           if(!(data.mgr_first_name === undefined))
           $('#mgr_first_name').val(data.mgr_first_name);
         });
       }
     );
   });

【问题讨论】:

  • Tl:dr 但是你不能在函数 1 的末尾调用函数 2 吗?
  • 你能告诉我我该怎么做吗? @马克斯·威廉姆斯

标签: javascript ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

这个怎么样(假设你有数据作为对象而不是数组,即使你有,你也可以使用Enumerable),你不必为此使用Javascript Ajax,

def populate_form
  @visual = Visual.find_by_id(params[:emp_id])
  @emp_first_name = @visual.first_name
  @person_id = @visual.pds_alias
  @manager_name = Manager.find_by_person_id(@person_id).mgr_name

    render :json => {

        :emp_first_name => @emp_first_name,
        :person_id => @person_id,
        :mgr_first_name => @manager_name

    }
 end

【讨论】:

  • 我试过了,但它看到 person_id 字段为空。管理器负载(40.6 毫秒)选择“EMP_DIRECT”。* FROM “EMP_DIRECT”,其中“EMP_DIRECT”。“PERSON_ID”为 NULL 并且 ROWNUM
猜你喜欢
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 2012-09-06
  • 1970-01-01
  • 2021-06-10
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多