我正在尝试使DataTables上的 Ryan Bates RailsCast 适应我的项目,但使用服务器端处理。当我调用索引视图时,标题显示正常,但表体显示“表中没有可用数据”;但是我有> 10K的记录。我的项目与示例的不同之处在于显示的主要对象是基因型,有两个相关的表。这是我的(初始部分)类 GenotypesDatables(类似于示例中的 ProductsDatables):
class GenotypesDatatable
delegate :params, :h, :link_to, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
# This is what feeds directly into DataTables
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Genotype.count,
iTotalDisplayRecords: 20,
aaData: data
}
end
private
def data
genotypes.map do |genotype|
[
# Note: h is shorthand for html_escape
# Am unsure is this is correct way to call records; compare
# to 340-datatables example.
h(genotype.gmarker.marker),
h(genotype.gsample.labid),
h(genotype.gsample.subjectid),
h(genotype.gsample.box),
h(genotype.gsample.well),
h(genotype.allele1),
h(genotype.allele2),
h(genotype.run_date)
]
end
end
这是基因型控制器的(相关部分):
class GenotypesController < ApplicationController
# GET /genotypes
# GET /genotypes.json
def index
respond_to do |format|
format.html
format.json { render json: GenotypesDatatable.new(view_context) }
end
end
我不知道这是否足以诊断问题,但如果有人熟悉这个优秀的 gem(它非常适合我的项目),并且需要查看更多代码,请告诉我。(我一直在如此,但找不到答案..)谢谢!
[添加编辑,基于评论]
这是 GenotypeDatatables 的完整类:
class GenotypesDatatable
delegate :params, :h, :link_to, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
# This is what feeds directly into DataTables
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Genotype.count,
iTotalDisplayRecords: 20,
aaData: data
}
end
private
def data
genotypes.map do |genotype|
[
# Note: h is shorthand for html_escape
# Am unsure is this is correct way to call records; compare
# to 340-datatables example.
h(genotype.gmarker.marker),
h(genotype.gsample.labid),
h(genotype.gsample.subjectid),
h(genotype.gsample.box),
h(genotype.gsample.well),
h(genotype.allele1),
h(genotype.allele2),
h(genotype.run_date)
]
end
end
def genotypes
@genotypes ||= fetch_genotypes
end
def fetch_genotypes
genotypes = Genotype.order("#{sort_column} #{sort_direction}")
genotypes = genotypes.page(page).per_page(per_page)
if params[:sSearch].present?
genotypes = genotypes.where("labid like :search or category like :search", search: "%#{params[:sSearch]}%")
end
end
def page
params[:iDisplayStart].to_i/per_page + 1
end
def per_page
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
end
def sort_column
columns = %w[allele1 allele2 run_date]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
这是 /app/assets/javascript/genotypes.js.coffee 中的 javascript 文件:
jQuery ->
$('#genotypes').dataTable
bJQueryUI: true
bProcessing: true
bServerSide: true
bDeferRender: true
"sAjaxSource": $('#genotypes').data('source')
这是我试图呈现我的数据表的视图:
<h1>Listing genotypes</h1>
<table id="genotypes" class="display" data-source="<%= genotypes_url(format: "json") %>">
<thead>
<tr>
<th>Marker</th>
<th>LabID</th>
<th>SubjectID</th>
<th>Box</th>
<th>Well</th>
<th>Allele1</th>
<th>Allele2</th>
<th>Run date</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<br />
如果需要更多代码,请告诉我...谢谢!