Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Hansontable on rails从json响应加载数据_Javascript_Jquery_Json_Ruby On Rails 4_Handsontable - Fatal编程技术网

Javascript Hansontable on rails从json响应加载数据

Javascript Hansontable on rails从json响应加载数据,javascript,jquery,json,ruby-on-rails-4,handsontable,Javascript,Jquery,Json,Ruby On Rails 4,Handsontable,如果我有一个返回json的控制器,我如何使用jquery将json数据结果加载到页面中,我的网页也使用远程true,因此请求将是ajax,结果json返回到页面,但现在我需要将json数组加载到handsontable中。我需要将json调用返回的数据绑定到handsontable对象中 class GradesController < ApplicationController def index logger.debug params respond_to do |

如果我有一个返回json的控制器,我如何使用jquery将json数据结果加载到页面中,我的网页也使用远程true,因此请求将是ajax,结果json返回到页面,但现在我需要将json数组加载到handsontable中。我需要将json调用返回的数据绑定到handsontable对象中

class GradesController < ApplicationController
  def index
    logger.debug params
    respond_to do |format|
      format.html
      format.json { render json: GradesDatatable.new(view_context)  }
      #format.js { @result = GradesDatatable.new(view_context).as_json }
    end
  end
end

为什么不在JS中对数据进行ajax调用呢?你的JS现在正在将数据设置为一个空数组,然后尝试从中获取数据,这显然是未定义的。好的想法是,提交按钮调用json并以某种方式在数据数组中进行设置,但我正在尝试找出如何,我需要以某种方式在JS中设置json。是的,没错。单击按钮时进行ajax调用,并在回调中将应答设置为数据。该按钮已经在使用属性remote:true进行ajax调用,我需要的是如何在数据中设置json响应。您可以使用handsontable使用的格式“设置响应”。它应该是一个数组数组,其中每个内部数组按顺序包含每列的值。或者,您可以使用一个映射,其中每个键都是列名,值是按顺序排列的每行值的数组。
<div class="row">
<%= simple_form_for :filters, url: grades_index_path('json'), method: :get, :remote => true do |f| %>
    <div class="small-4 columns">
        <%= f.input :curso, collection: Curso.all, :label_method => "nombre", :value_method => "nombre" %>
        <%= f.input :seccion, collection: Seccion.all, :label_method => "nombre", :value_method => "nombre" %>
        <%= f.collection_radio_buttons :tanda, [['M', 'Matutina'] ,['D', 'Despertina'],['N', 'Nocturna']], :first, :last, :checked => 'M' %>
    </div>
    <div class="small-3 columns">
       <div class="form-actions">
          <%= f.button :submit, "Filtrar", :class => "button large" %>
       </div>   
    </div>
<% end %>


</div>

<div class="row">
    <div id="grades"></div>
</div>
  $(function(){

  data = []
  window.data = data.aaData
  var container = document.getElementById('grades');
  window.hot = new Handsontable(container,
    {
      data: window.data,
      ...