本文介绍如何在 Bootstrap Table 中启用服务端分页,通过配置 sidePagination 为 server 实现前后端数据交互。重点解析前端传递的 limit 和 offset 参数含义,以及如何结合 Spring MVC 接收分页数据并完成服务器端数据处理。
Bootstrap Table 服务端分页配置与参数解析
在使用 Bootstrap Table 进行客户端分页时,借助相关文档资源,可以明确传递给后台的关键参数:每页条数 Limit 和记录起始位置 Offset。基于此,对代码进行封装,主要功能是从 Bootstrap Table 中获取当前页码及每页数量,并将其传递给后台进行处理。
$('#table').bootstrapTable({
url: '<%=path%>/FeedList.cqzk',
striped: true,
pagination: true,
pageList: [3,5,20],
pageSize:3,
pageNumber:1,
sidePagination:'server',//设置为服务器端分页
columns: [{
field: 'title',
title: '标题'
}, {
field: 'creatTime',
title: '时间'
} ]
});
@RequestMapping(value = "/FeedList.cqzk")
@ResponseBody
public String url_ad1(HttpServletRequest request,BootPage page)
throws ServletException,IOException,RuntimeException{
@SuppressWarnings("unchecked")
// List<Feedback> list = feedBackDao.find("from Feedback");
BootPage pager = feedBackDao.getByPage("from Feedback",page,null);
System.out.println((JSONArray.fromObject(pager)).getString(0).toString());
return (JSONArray.fromObject(pager)).getString(0).toString();
// 不写.getString(0) 就多一个中括号,返回的就是数组,写了就是返回第一个对象。
}
public BootPage getByPage(String hql,BootPage pager,Map<String, Object> condition){
if (pager == null) {
throw new IllegalArgumentException("分页 不能为空!");
}
Query q = sessionFactory.getCurrentSession().createQuery(hql);
q.setFirstResult(pager.getOffset());
q.setMaxResults(pager.getLimit());
if (condition != null) {
q.setProperties(condition);
}
pager.setRows(q.list());
pager.setTotal(this.countAll(hql, condition));
return pager;
}
protected Long countAll(String hql, Map<String, Object> condition) {
if (hql == null) {
return 0l;
}
String tmpHql = hql.toLowerCase();
String regex = hql.substring(0, tmpHql.indexOf("from"));
hql = hql.replaceFirst(regex, "select count(*) ");
Query q = sessionFactory.getCurrentSession().createQuery(hql);
if (condition != null) {
q.setProperties(condition);
}
return (Long) q.uniqueResult();
}
public final class BootPage<T> {
protected Long total;
protected List<T> rows;
protected int limit=0;
protected int offset = 0;
protected String order ="asc" ;
若需进一步深入掌握,可参考相关学习资源,以下提供三个补充专题供深入学习。
Bootstrap 基础学习教程
Bootstrap 实战应用教程
Bootstrap 插件使用指南
以上即为 Bootstrap Table 的使用方法分享,旨在帮助读者更好地掌握该组件的应用技巧。