简体   繁体   English

数据表导轨问题

[英]Datatable rails issue

I have rails application that is using datatables. 我有正在使用数据表的Rails应用程序。 I'm using the jquery-datatables-rails gem. 我正在使用jquery-datatables-rails gem。

I have a index.html.erb that looks like: 我有一个index.html.erb看起来像:

<table id='products' class="display" data-source="<%= store_products_path(format: "json") %>">
  <thead>
    <tr>
      <th>Store Id</th>
      <th>Created At</th>
      <th>Updated At</th>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>

I have a products controller that looks like: 我有一个产品控制器,看起来像:

@products = Product.all
respond_to do |format|
    format.html
    format.json do
      render :json=> {
        "sEcho"               => params[:sEcho].to_i,
        "iTotalRecords"       => @products.count,
        "iTotalDisplayRecords"=> @products.count,
        "aaData"              => @products.as_json(
          :only => [:store_id, :created_at, :updated_at]
        )
      }
    end
  end

that returns the following json: 返回以下json:

{"sEcho":0,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[{"store_id":128,"created_at":"2014-02-19T04:30:43.455Z","updated_at":"2014-02-19T04:30:43.455Z"},{"store_id":128,"created_at":"2014-02-22T04:39:08.708Z","updated_at":"2014-02-22T04:39:08.708Z"}]}

I also have a products.js file: 我也有一个product.js文件:

jQuery(function() {
   return $('#products').dataTable({
     sPaginationType: "full_numbers",
     bJQueryUI: true,
     bProcessing: true,
     bServerSide: true,
     sAjaxSource: $('#products').data('source')
   });
});

The datatable is showing up, but after it loads an alert popup comes up that says: DataTables warning (table id = 'products'): Requested unknown parameter '0' from the data source for row 0 该数据表正在显示,但是在加载后出现了一个警告弹出窗口,提示: DataTables警告(表ID ='products'):从数据源请求的第0行的未知参数'0'

How can I fix this? 我怎样才能解决这个问题? Thanks for all help! 感谢所有帮助!

<table id='products' class="display" data-source="<%= store_products_path(format: "json") %>">
  <thead>
    <tr>
      <th data-column='store_id'>Store Id</th>
      <th data-column='created_at'>Created At</th>
      <th data-column='updated_at'>Updated At</th>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>


jQuery(function() {
   columns = []
   $('#products').find('thead tr th').each(function(i, th){
     columns.push({mData: $(th).data('column')})
   });
   return $('#products').dataTable({
     sPaginationType: "full_numbers",
     bJQueryUI: true,
     bProcessing: true,
     bServerSide: true,
     aoColumns: columns,
     sAjaxSource: $('#products').data('source')
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM