Javascript DataTableRails问题
我有一个使用datatables的rails应用程序。我正在使用jquerydatatables 我有一个index.html.erb,看起来像:Javascript DataTableRails问题,javascript,jquery,ruby-on-rails,ruby,datatable,Javascript,Jquery,Ruby On Rails,Ruby,Datatable,我有一个使用datatables的rails应用程序。我正在使用jquerydatatables 我有一个index.html.erb,看起来像: <table id='products' class="display" data-source="<%= store_products_path(format: "json") %>"> <thead> <tr> <th>Store Id</th>
<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>
@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
返回以下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"}]}
我还有一个products.js文件:
jQuery(function() {
return $('#products').dataTable({
sPaginationType: "full_numbers",
bJQueryUI: true,
bProcessing: true,
bServerSide: true,
sAjaxSource: $('#products').data('source')
});
});
datatable正在显示,但加载后会出现一个警报弹出窗口,显示:
DataTables警告(表id='products'):从数据源为第0行请求未知参数“0”
我怎样才能解决这个问题?谢谢你的帮助
<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')
});
});
商店Id
创建于
更新于
jQuery(函数(){
列=[]
$('#products')。查找('thead tr th')。每个(函数(i,th){
columns.push({mData:$(th).data('column')})
});
return$(“#products”).dataTable({
sPaginationType:“完整编号”,
bJQueryUI:是的,
b处理:对,
观察家方面:是的,
aoColumns:columns,
sAjaxSource:$(“#产品”).data('source')
});
});