1 . 在datatables 中加入
"sPaginationType" : "full_numbers" ,
"bServerSide": true,
2个参数。"bServerSide": true, 必须要加。否则不会传递相关参数给后台
2. 传入后台的相关 参数 和 后台需要传出的参数 可以参考 api 文档 http://datatables.net/usage/server-side
| int | iDisplayStart | Display start point in the current data set. |
| int | iDisplayLength | Number of records that the table can display in the current draw. It is expected that the number of records returned will be equal to this number, unless the server has fewer records to return. |
3. rails 后台进行 分页的逻辑处理
def index
idisplaystart = params[:iDisplayStart]
idisplaylength = params[:iDisplayLength]
secho = params[:sEcho]
itotalrecords = GisRoadLink.where("activate = ?",params[:activate]).count()
items = GisRoadLink.where("activate = ?",params[:activate]).order("id asc").limit(idisplaylength).offset(idisplaystart)
for item in items do
item_array = []
item_array << item.id
item_array << item.no
....
end
json_hash.store("aaData",items_array)
json_hash.store("iTotalRecords",itotalrecords)
json_hash.store("iTotalDisplayRecords",itotalrecords)
json_hash.store("sEcho",secho)
render json: json_hash
end
本文介绍如何在Datatables中实现服务器端分页,并详细解释了必要的参数配置。通过设置&sPaginationType=full_numbers及&bServerSide=true,确保Datatables能正确地与后端交互。文章还提供了Rails后端分页逻辑的具体实现,包括如何解析前端传来的分页参数,如iDisplayStart和iDisplayLength,并展示如何返回符合Datatables格式的数据。
420

被折叠的 条评论
为什么被折叠?



