Dudo
Dudo

Reputation: 4169

Cannot read property 'asSorting' of undefined - DataTables

for ( j=0, jLen=oColumn.asSorting.length ; j<jLen ; j++ ) at line 6706 of DataTables...

I copied the railscast on the topic, pretty much verbatim. So oColumn is undefined. The interwebs tell me that I need to have <thead> and <th> values...

view:

<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>

Here is a copy of the new class. Again, pretty much copied the 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 (coffee):

jQuery ->
  $('#companyBoxList').dataTable
    sPaginationType: "full_numbers"
    bJQueryUI: true
    bProcessing: true
    bServerSide: true
    sAjaxSource: $('#companyBoxList').data('source')

I was able to get this to process, by adding "aoColumns": [null, null, null, null, null, null]. This nulls out the headers though, which defeats the purpose. This points to an issue with the headers being read though, and not the json, as the data returns just fine.

ideas?

Upvotes: 4

Views: 11926

Answers (2)

Dudo
Dudo

Reputation: 4169

Syntax error... was missing a closing > on my initial table call. Check the very first line of my code.

Upvotes: 1

OQJF
OQJF

Reputation: 1350

If I'm not wrong that you want to bind the column data to the json, so you should change you code to this:

$('#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
               ]

Upvotes: 0

Related Questions