所以我初始化了datatables gem ok并且它正常工作。我现在要弄清楚的是如何实现ajax api以便自动更新字段。
以下是我在application.js中的内容
$('#usertableadmin').DataTable({
serverSide: true,
ajax: 'users/',
dom: 'T<"clear">lfrtip',
tableTools: {
"sSwfPath": "http://localhost:3000/swf/copy_csv_xls_pdf.swf",
"aButtons": [
"copy",
"print",
{
"sExtends": "collection",
"sButtonText": "Export",
"aButtons": [ "csv", "xls",
{
"sExtends": "pdf",
"sPdfOrientation": "landscape",
"sPdfMessage": "Exported Users from website Database"
}
]
},
]
},
responsive: true
}).ajax.reload();
这是我的usercontroller索引操作,负责显示用户
def index
@users = User.all
respond_to do |format|
format.html #new.html.erb
format.json { render json: @users}
end
# @columns = User.column_names
end
这是我的index.html.erb中的html
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= link_to "Back", admin_path %>
<table class="display responsive no-wrap text-center" id="usertableadmin">
<thead>
<tr>
<th>id</th>
<th>Username</th>
<th>Email</th>
<th>Activated?</th>
<th>Admin?</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= best_in_place user, :name, url: "users/#{user.id}", cancel_button: 'X' %></td>
<td><%= best_in_place user, :email, url: "users/#{user.id}" %></td>
<td><%= best_in_place user, :activated, url: "users/#{user.id}" %></td>
<td><%= best_in_place user, :admin, url: "users/#{user.id}" %></td>
</tr>
<% end %>
</tbody>
</table>
虽然使用此配置列出了所有用户,但分页不起作用,我也在控制台中收到此错误
Uncaught TypeError: Cannot read property 'length' of undefined
我已经阅读了数据表ajax api reload方法,但似乎无法找到我的错误。