显示AJAX查询的计算结果

时间:2013-09-09 07:12:01

标签: javascript ruby-on-rails ajax

在此ROR应用程序中,如果用户更改任何输入值,我将尝试显示/重新显示结果计算。使用Chrome上的开发者工具我可以确定已使用下面的Javascript计算并返回了相应的结果。我能够选择任何输入值的变化并重做计算。但是我无法让Javascript显示结果。

下面我添加了Javascript,视图的相关部分(一个主要部分 - 一个部分)以及控制器的相关部分。

javascript - drill.js.coffee 我有理由相信我错过了一些能够让我将相应的代码放在我指示的位置的东西。

$('document').ready ->
  if $('#evaluation_assumption_params').length == 1
    drill_id = $('.form.assumption').attr('data-drillid')
    $('#evaluation_assumption_params').change((event) ->
      event.preventDefault()
      loadAssumptionChange drill_id
     )

loadAssumptionChange = (drill_id)->
  data = $('#evaluation_assumption_params').serialize()
  console.log data
  $.ajax
    url:"/drills/#{drill_id}/projection.json"
    type:"post"
    dataType: 'json'
    data: data
    success: (data,success,xhr)->

    # ??? - tried numerous statements including 
    # $('#place_to_add_projection_results').html(data)
    # $('#ajax').html("<%= escape_javascript(render(:partial => 'drills/evaluation_results')).html_safe %>")
      # What should go here ???

partial - _assessment_results.html.erb

<%= simple_form_for :result_list, :html => { class: 'infogroup', id: "evaluation_results", :method => :post },
      remote: true do |f| %>
  <%= content_tag :header, "Results", class: 'infogroup-header'%>  
  <%= content_tag :div, class: 'infogroup-body' do %> 
    <%= content_tag :table, class: 'info', border: 0, cellspacing: 5, cellpadding: 0 do %>     
      <tr>
        <th>Company</th>
        <th>SP</th>
        <th>Mkt Cap</th>
        ...
      </tr>
      <% if @result_list.present? %>
        <% @result_list.each do |result| %>                 
          <tr>
            <td><%= result.listing_name %></td>
            <td><%= number_to_currency(result.listing.share_price_dollars,
                  :precision => 3, :delimiter => ',')     %></td> 
     …

与控制器动作对应的主屏幕 - investor.html.erb 以上部分由此屏幕调用以及javascript

<% content_for :title, "Drill Investor - Valuations" %>
… more heading stuff
<section>
  <div class="form assumption" data-drillid="<%= @drill.id %>">
      <%= render 'assumption_params' %>
  </div>

  <div class="form">
      <div id="place_to_add_projection_results">
        <%= render 'evaluation_results' %>
      </div>
  </div>

控制器

class DrillsController < ApplicationController
  before_filter :current_user
  ...  
  def investor
      # screens associated allow entry of data as well as display of the results
  end

  def projection
    result_list = Evaluation.generate_projection(@drill.drill_interests, assumption_params)
    render json: result_list
  end

任何帮助非常感谢

皮尔

1 个答案:

答案 0 :(得分:0)

很可能你得到一个列表,所以你必须迭代结果并将每个结果附加到容器div,为简单起见,我会在你的成功ajax回调中做类似的事情:

var $container_div = $("#place_to_add_projection_results");
$container_div.empty(); // First clear div just in case
var evaluations = xhr.responseJSON;
$.each(evaluations, function(i, evaluation) {
  $container_div.append("<div"> + evaluation.X + ", " + evaluation.Z +" </div>");
});

其中X,Z是您模型中的某些属性。