【问题标题】:Rails 4 + Datatables: Ajax-datatables-rails gem will not update tableRails 4 + Datatables:Ajax-datatables-rails gem 不会更新表
【发布时间】:2016-05-18 18:33:57
【问题描述】:

我正在尝试使用here 找到的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

数据表

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.
});

【问题讨论】:

  • 您的$('#users-table').dataTable() 调用是否在DOMReady 回调中?
  • 是的,并且它与许多其他函数一起被成功调用。图表加载,只是不会更新,ajax 请求看起来都一样

标签: jquery ruby-on-rails ruby ajax datatable


【解决方案1】:

您启用了服务器端处理,这意味着当您搜索或排序并通过您在 ajax 选项中指定的 url 传递一组参数时,数据表将发出 ajax 请求。

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

【讨论】:

  • 我明白这一点,但据我所知,这个 gem 应该可以帮助发出这些 ajax 请求......
  • 啊抱歉没有看到你在使用 gem。
【解决方案2】:

尝试在您的所有列的 UserDatatable 类中使用单数“user.id”而不是“users.id”。我必须为我这样做,因为它不接受我的表的典型复数命名约定,如该 gem 的文档中所示。

【讨论】:

  • 您应该能够通过致电users_path{ format: :json} 来检查是否存在问题。例如。如果 users_path 是 /users 那么调用 localhost:3000/users.json 应该会显示一个 json 格式的数据表。如果这表明我怀疑是UserDatatable 这就是问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2017-11-05
相关资源
最近更新 更多