4

for ( j=0, jLen=oColumn.asSorting.length ; j<jLen ; j++ )在数据表的第 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

box_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,因为数据返回得很好。

想法?

4

2 回答 2

1

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

于 2014-03-04T00:14:49.247 回答
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
               ]
于 2013-03-15T01:04:21.027 回答