2016-05-18 86 views
2

我正在尝试使用ajax-datatables-rails宝石找到here没有运气。我的表格显示出来,并且具有正确的数据(对于第一页),但是当我尝试搜索,排序或更改页面时,没有表格数据被更新。Rails 4 + Datatables:Ajax-datatables-rails gem不会更新表

我错过了一些简单的东西吗?

查看

<table id="users-table" data-source="<%= users_path(format: :json) %>"> 
    <thead> 
    <tr> 
     <th>ID</th> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Admin?</th> 
     <th>Employee?</th> 
     <th>Client?</th> 
    </tr> 
    </thead> 
    <tbody> 
    </tbody> 
</table> 

控制器

def index 
    respond_to do |format| 
     format.html 
    format.json { render json: UserDatatable.new(view_context) } 
    end 
end 

DATATABLE

class UserDatatable < AjaxDatatablesRails::Base 
    # uncomment the appropriate paginator module, 
    # depending on gems available in your project. 
    # include AjaxDatatablesRails::Extensions::Kaminari 
    include AjaxDatatablesRails::Extensions::WillPaginate 
    # include AjaxDatatablesRails::Extensions::SimplePaginator 

    def sortable_columns 
    # list columns inside the Array in string dot notation. 
    # Example: 'users.email' 
     @sortable_columns ||= [ 
      'users.id', 
      'users.name', 
      'users.email', 
      'users.admin', 
      'users.employee', 
      'users.is_client' 
     ] 
    end 

    def searchable_columns 
    # list columns inside the Array in string dot notation. 
    # Example: 'users.email' 
     @searchable_columns ||= [ 
      'users.id', 
      'users.name', 
      'users.email', 
      'users.admin', 
      'users.employee', 
      'users.is_client' 
     ] 
    end 

    private 

    def data 
    records.map do |record| 
     [ 
     # comma separated list of the values for each cell of a table row 
     # example: record.attribute, 
       record.id, 
       record.name, 
       record.email, 
       record.admin, 
       record.employee, 
       record.is_client 
     ] 
    end 
    end 

    def get_raw_records 
    # insert query here 
     User.all 
    end 

    # ==== Insert 'presenter'-like methods below if necessary 
end 

个JS

$('#users-table').dataTable({ 
    "processing": true, 
    "serverSide": true, 
    "ajax": $('#users-table').data('source') 
    "pagingType": "full_numbers" 
    // optional, if you want full pagination controls. 
    // Check dataTables documentation to learn more about 
    // available options. 
}); 
+0

是否在DOMReady回调函数中调用$('#users-table')。dataTable()'调用? – Uzbekjon

+0

是的,它与一堆其他功能被称为成功。该图表加载,它只是不会更新,并且ajax请求都看起来相同 –

回答

0

你必须启用服务器端处理,这意味着数据表将当您搜索或排序,并通过您在阿贾克斯选项中指定的URL传递一组参数的Ajax请求。

实际处理必须在服务器上完成,并且响应必须符合数据表指定的协议。您可以参考文档here

+0

我明白,但是这个宝石应该有助于使这些ajax请求,据我所知... –

+0

啊对不起,没有看到你是使用宝石。 –

0

尝试在您的UserDatatable类中为所有列使用单数'user.id'而不是'users.id'。我不得不这样做我的表不会接受我的表的典型复数命名约定,如该宝石的文档中所示。

+0

您应该可以通过调用'users_path {format :: json}'来查看是否有问题。例如。如果users_path是'/ users',那么调用'localhost:3000/users.json'应该会显示一个json格式的数据表。如果这表明我怀疑这是'UserDatatable'这个问题。 – jQwierdy