无法读取未定义的属性'asSorting' - DataTables

4

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,因为数据返回正常。
有什么想法吗?

我不熟悉Rails,但是datatables可以,你能简短地解释一下问题吗? - OQJF
如果我在我的JavaScript中不加入aoColumns:[null,null,null,null,null,null],它会报错,声称找不到oColumn对象。 - Dudo
好的,我知道原因了。请参考我的答案,如果有其他问题,请在评论中提出,让我们一起前进。顺便说一句,据我所知,Rail非常棒。 - OQJF
哦,我真蠢。在<table>调用的开头缺少了>。哇。 - Dudo
我不认为原因是那个,但也许是。我发布了一些更改后的代码,希望能给你一些想法。同时注意从服务器返回的JSON格式。 - OQJF
2个回答

1

语法错误...我的初始表格调用缺少一个闭合的 >。请检查我的代码的第一行。


0

如果我没弄错的话,你想把列数据绑定到JSON,所以你应该将代码改成这样:

$('#companyBoxList').dataTable
    sPaginationType: "full_numbers",
    bJQueryUI: true,
    bProcessing: true,
    bServerSide: true,
    sAjaxSource: $('#companyBoxList').data('source'),
    aoColumns: [{"mDataProp": "uId"},
                 .....the other data that you want to add such as length and width
               ]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接