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

标签 ruby-on-rails ruby-on-rails-3 datatables

for ( j=0, jLen=oColumn.asSorting.length ; j<jLen ; j++ )在 DataTables 的第 6706 行...

我复制了railscast关于主题,几乎逐字逐句。所以oColumn未定义。互联网告诉我,我需要 <thead><th>值(value)观...

查看:

<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] 来处理它。但这会使 header 无效,从而达不到目的。这指出了读取 header 的问题,而不是读取 json 的问题,因为数据返回得很好。

想法?

最佳答案

语法错误...在我的初始表调用中缺少结束符 >。检查我的代码的第一行。

关于ruby-on-rails - 无法读取未定义的属性 'asSorting' - DataTables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422638/

相关文章:

ruby-on-rails - Rake 测试仅在测试环境中失败

jquery - 数据表 DOM 定位

javascript - 如何使Jquery DataTable列作为具有其他列值的HyperLink或ActionLink?

ruby-on-rails - Rake 和未初始化常量

ruby-on-rails - 解决 Rails 应用程序中的类名冲突

ruby-on-rails-3 - Rails3 : Render an in-memory image in the view

ruby-on-rails - ActiveAdmin -- 如何从部分访问实例变量?

rake db :create 后 MySql 错误 1045

javascript - 更改数据表中ajax请求中下拉菜单的选择选项

ruby-on-rails - 意外的 keyword_end,期待 $end(SyntaxError)