for ( j=0, jLen=oColumn.asSorting.length ; j<jLen ; j++ )
在 DataTables 的第 6706 行...
我基本上逐字逐句地复制了关于这个主题的railscast。因此,oColumn
未定义。网络告诉我需要有 <thead>
和 <th>
值...
视图:
<table id="companyBoxList" class="display" data-source="<%= comp_boxes_path(format: "json") %>"
<thead>
<tr>
<th>uid</th>
<th>length</th>
<th>width</th>
<th>height</th>
<th>weight</th>
<th>trips</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
这是一个新类的副本。大部分内容都是从railscast中复制过来的。
boxes_database.rb
class BoxesDatatable
delegate :params, :h, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Box.count,
iTotalDisplayRecords: boxes.count,
aaData: data
}
end
private
def data
boxes.map do |box|
[
box.uid,
box.length,
box.width,
box.height,
box.weight,
box.trips
]
end
end
def boxes
@boxes ||= fetch_boxes
end
def fetch_boxes
boxes = Box.order("#{sort_column} #{sort_direction}")
boxes = boxes.page(page).per(per_page)
if params[:sSearch].present?
boxes = boxes.where("uid like :search or trips like :search", search: "%#{params[:sSearch]}%")
end
boxes
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[uid length width height weight trips]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
Javascript (咖啡)
jQuery ->
$('#companyBoxList').dataTable
sPaginationType: "full_numbers"
bJQueryUI: true
bProcessing: true
bServerSide: true
sAjaxSource: $('#companyBoxList').data('source')
我在这个问题上有了一个解决方法,通过添加
"aoColumns": [null, null, null, null, null, null]
来处理。但是,这将清空标题,失去了预期的效果。这表明标题被读取时存在问题,而不是JSON,因为数据返回正常。有什么想法吗?
aoColumns:[null,null,null,null,null,null]
,它会报错,声称找不到oColumn对象。 - Dudo<table>
调用的开头缺少了>
。哇。 - Dudo