2012-08-15 52 views
0

我试图使用simple_datatables宝石,但the docs不是很清楚。的Rails 3.2和simple_datatables - 为什么不是我的表加载?

当我加载在我的浏览器search.datatables和search.html,我得到JSON的内容,但是当我用我的索引视图,是没有得到填充我的表。

我创建的应用程序/视图/行业/与此内容search.jsonify:

@trades.each do |t| 
    json << [ 
    t.tddt, 
    ... # omitted for brevity 
    t.sdbk, 
    ] 
end 

而且我已经添加了 “搜索” 控制器行动,我TradesController:

respond_to :html, :datatables 

def search 
    @trades = Trade.search(params[:search]) 
    respond_with @trades 
end 

def index 
    respond_to do |format| 
    format.html # index.html.erb 
    end 
end 

同样,如文档所示,我已将此javascript添加到我的应用/ views/index.html.erb:

<script type="text/javascript"> 

$("#trades_tbl").dataTable({ 
    "sAjaxSource" : "/trades/search.datatables", 
    "aaSorting" : [[0,'asc']], 
    "aoColumns" : [ 
     {"sName":"tddt"}, 
     ... // again omitted for brevity 
     {"sName":"sdbk"}, 
    ], 
    "bServerSide" : true, 
    "fnServerData" : simpleDatatables 
    }); 

</script> 

这里是在同一个文件中的表格:

<table id="trades_tbl"> 
    <thead> 
    <tr> 
     <th>Tddt</th> 
     <!-- more columns omitted for brevity --> 
     <th>Sdbk</th> 
    </tr> 
    </thead> 

    <tbody/> 
</table> 

那么我错过了什么?为什么不是JavaScript加载我的表?

谢谢。

注意:如果某个具有1500+声望的人会创建simple_datatables标签,我认为这将是一件好事。

回答

0

一些想法,我有:

要确认,您添加以下到您的application.js资产管道清单文件:

//= require simple_datatables 

并重新启动Web服务器?

另一个可能的障碍是里面的index.html前的文件执行JavaScript被加载完成,可能要包装一个$(文件)。就绪回调,像这里面的里面的index.html的JavaScript:

$(document).ready(function(){ 
    $("#trades_tbl").dataTable({ 
    .... 
    }); 
}); 

最终的想法:没有网络浏览器的开发控制台显示有发生的任何JavaScript错误?如果是这样,你可能想要解决这些问题。

+0

我无法相信我忘了用$(文件)。就绪回调来包装的JavaScript。非常感谢。 – 2012-08-15 15:00:15