1

我正在尝试实现覆盖数据表的 Railscast 稍微更高级的版本。

当控制器通过 iteslf 提供 WaresDatatable.new(view_context) 响应时,我能够通过 ajax 使表工作,这是一个胜利。

但是,当我尝试扩展我正在做的事情并嵌套 json 以便数据表和其他函数可以使用 json 响应时,ajax 数据表不再加载数据。

我对此相对较新,并且针对这个看似很小的问题已经有一段时间了。

控制器:

 respond_to do |format|
  format.html # show.html.erb
  #format.json { render :json => @contributor }
format.json { 
                    render :json => {
                        :warestable => WaresDatatable.new(view_context),
                        :contributor => @contributor

                     }
             }      
end

wares_datatable.rb (根据 railscasts 并在 json 未嵌套时工作)

    class WaresDatatable
  delegate :params, :h, :link_to, :admin_signed_in?, :edit_ware_path, :current_user, to: :@view

  def initialize(view)
    @view = view
  end

  def as_json(options = {})
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: Ware.count,
      iTotalDisplayRecords: wares.count,
      aaData: data
    }
  end

private

  def data

    if admin_signed_in?
     wares.map do |product|
      [
        link_to(product.name, product),
        product.origin,
        product.chron_range,
        product.desc,
        link_to("View", product) + " "+link_to("Edit", edit_ware_path(product), :class => 'btn btn-mini') + " " +link_to("Delete", product, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini btn-danger')

      ]
    end

    else

     wares.map do |product|
      [
        link_to(product.name, product),
        product.origin,
        product.chron_range,
        product.desc,
        link_to("View", product)
      ]
    end


   end 

  end


  def wares
    @wares ||= fetch_wares
  end



  def page
    params[:iDisplayStart].to_i/per_page + 1
  end



  def per_page
    params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 1
  end

    def fetch_wares

    contributor = Contributor.find(params[:id])



    wares = contributor.wares.order("#{sort_column} #{sort_direction}")

    wares = wares.page(page).per_page(per_page)
    #wares = wares.user.contributor
    if params[:sSearch].present?
      wares = wares.where("name like :search or origin like :search or desc like :search", search: "%#{params[:sSearch]}%")
    end
    wares
  end

  def sort_column
    columns = %w[name origin chron_range desc action]
    columns[params[:iSortCol_0].to_i]
  end

  def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
  end
end

JS.Coffee 文件

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

JSON 响应

{"warestable":{"sEcho":0,"iTotalRecords":22,"iTotalDisplayRecords":1,"aaData":[["<a href=\"/wares/1\">crap</a>",null,"old","really crappy condition","<a href=\"/wares/1\">View</a>"]]},"contributor":{"ad_board":true,"avatar_content_type":"image/jpeg","avatar_file_name":"success.jpg","avatar_file_size":90652,"avatar_updated_at":"2013-05-04T01:54:52Z","created_at":"2013-05-01T05:19:51Z","email":"jack@jack.com","first_name":"jack","id":1,"last_name":"frost","name":null,"ptrgrph_id":null,"resume_content_type":"image/png","resume_file_name":"Argentina.png","resume_file_size":42260,"resume_updated_at":"2013-05-04T03:17:50Z","searchable":"jack frost jack@jack.com bu","selfdescription":"<p><span style=\"font-family:comic sans ms,cursive\">This is my new website info</span></p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p><span style=\"font-family:comic sans ms,cursive\">Hhaahaha</span></p>\r\n","university":"bu","updated_at":"2013-05-08T01:58:58Z","user_id":4,"ware_id":null}}

Datatables 能够处理嵌套资源,所以我很确定它只是我。如果有人能指出我正确的方向,非常感谢。

4

1 回答 1

0

我知道这是一个老问题,但请查看sAjaxDataProp

它允许您设置自定义属性而不是 aaData,但我还没有找到一种方法来告诉数据表在sEcho其他地方寻找等。

于 2014-09-03T08:52:26.597 回答