4

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?

Dudo
  • 4,002
  • 8
  • 32
  • 57
  • I don't know Rail, but the datatables is ok, can you explain the problem for short? – OQJF Mar 15 '13 at 00:58
  • without putting `aoColumns: [null, null, null, null, null, null]` in my javascript, it errors out, claiming it can't find the oColumn object. – Dudo Mar 15 '13 at 01:00
  • OK, I know what the reason is. refer to my answer, if have other problem just comment let, let's go forward together and BTW, as I know Rail is awesome. – OQJF Mar 15 '13 at 01:01
  • OMG, I'm dumb. was missing the `>` on the beginning of the `` call. wow.
    – Dudo Mar 15 '13 at 01:05
  • I don't think that the reason is that, but maybe it is. I posted some changed code that hope can give you some ideas. also pay attention to json format that returned from server – OQJF Mar 15 '13 at 01:07

2 Answers2

1

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

Dudo
  • 4,002
  • 8
  • 32
  • 57
0

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
               ]
OQJF
  • 1,350
  • 9
  • 12