bootstrap table分页(前后端两种方式实现)
原文链接:http://blog.csdn.net/qq_37936542/article/details/79163125
bootstrap table分页有那种方式:
前端分页:一次性从数据库查询所有的数据,在前端进行分页(数据量小的时候或者逻辑处理不复杂的话可以使用前端分页)
服务器分页:每次只查询当前页面加载所需要的那几条数据
一:引入js、css文件
- <!-- 引入的css文件 -->
- <link href="../css/bootstrap.min.css" rel="stylesheet" />
- <link href="../css/bootstrap-table.min.css" rel="stylesheet">
- <!-- 引入的js文件 -->
- <script src="../js/jquery.min.js"></script>
- <script src="../js/bootstrap.min.js"></script>
- <script src="../js/bootstrap-table.min.js"></script>
- <script src="../js/bootstrap-table-zh-CN.min.js"></script>
- <div class="panel panel-default">
- <div class="panel-heading">
- 查询条件
- </div>
- <div class="panel-body form-group" style="margin-bottom:0px;">
- <label class="col-sm-1 control-label" style="text-align: right; margin-top:5px">姓名:</label>
- <div class="col-sm-2">
- <input type="text" class="form-control" name="Name" id="search_name"/>
- </div>
- <label class="col-sm-1 control-label" style="text-align: right; margin-top:5px">手机号:</label>
- <div class="col-sm-2">
- <input type="text" class="form-control" name="Name" id="search_tel"/>
- </div>
- <div class="col-sm-1 col-sm-offset-4">
- <button class="btn btn-primary" id="search_btn">查询</button>
- </div>
- </div>
- </div>
- <table id="mytab" class="table table-hover"></table>
- <div id="toolbar" class="btn-group pull-right" style="margin-right: 20px;">
- <button id="btn_edit" type="button" class="btn btn-default" style="display: none; border-radius: 0">
- <span class="glyphicon glyphicon-pencil" aria-hidden="true" ></span>修改
- </button>
- <button id="btn_delete" type="button" class="btn btn-default" style="display: none;">
- <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
- </button>
- <button id="btn_add" type="button" class="btn btn-default">
- <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
- </button>
- </div>
- $('#mytab').bootstrapTable({
- method: 'post',
- contentType: "application/x-www-form-urlencoded",//当请求方法为post的时候,必须要有!!!!
- url:ROOT+"user/getUserListPage",//请求路径
- striped: true, //是否显示行间隔色
- pageNumber: 1, //初始化加载第一页
- pagination:true,//是否分页
- sidePagination:'server',//server:服务器端分页|client:前端分页
- pageSize:10,//单页记录数
- pageList:[5,10,20,30],//可选择单页记录数
- showRefresh:true,//刷新按钮
- queryParams : function (params) {//上传服务器的参数
- var temp = {//如果是在服务器端实现分页,limit、offset这两个参数是必须的
- limit : params.limit, // 每页显示数量
- offset : params.offset, // SQL语句起始索引
- //page: (params.offset / params.limit) + 1, //当前页码
- Name:$('#search_name').val(),
- Tel:$('#search_tel').val()
- };
- return temp;
- },
- columns:[
- {
- title:'登录名',
- field:'loginName',
- sortable:true
- },
- {
- title:'姓名',
- field:'name',
- sortable:true
- },
- {
- title:'手机号',
- field:'tel',
- },
- {
- title:'性别',
- field:'sex',
- formatter: formatSex,//对返回的数据进行处理再显示
- }
- ]
- })
- //value代表该列的值,row代表当前对象
- function formatSex(value,row,index){
- return value == 1 ? "男" : "女";
- //或者 return row.sex == 1 ? "男" : "女";
- }
- //查询按钮事件
- $('#search_btn').click(function(){
- $('#mytab').bootstrapTable('refresh', {url: ROOT+'user/getUserListPage'});
- })
四:实现前端分页
(一)设置js中参数
- sidePagination:'client',
- queryParams : function (params) {
- var temp = {
- name:$('#search_name').val(),
- tel:$('#search_tel').val()
- };
- return temp;
- },
- package com.debo.common;
- public class User {
- private Integer id;
- private String loginName;
- private String name;
- private String tel;
- private Integer sex;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getLoginName() {
- return loginName;
- }
- public void setLoginName(String loginName) {
- this.loginName = loginName;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getTel() {
- return tel;
- }
- public void setTel(String tel) {
- this.tel = tel;
- }
- public Integer getSex() {
- return sex;
- }
- public void setSex(Integer sex) {
- this.sex = sex;
- }
- }
- /**
- *直接一次性查出所有的数据,返回给前台
- */
- @RequestMapping("/getUserListPage")
- @ResponseBody
- public List<User> getUserListPage(User user,HttpServletRequest request){
- List<User> list = userService.getUserListPage(user);
- return list;
- }
- <select id="getUserListPage" resultType="com.debo.common.User">
- SELECT * FROM user WHERE 1 = 1
- <if test="name!=null and name !=''">
- AND name LIKE CONCAT('%',#{name},'%')
- </if>
- <if test="tel!=null and tel !=''">
- AND tel = #{tel}
- </if>
- </select>
五:实现服务器端分页
(一)设置js中参数
- sidePagination:'server',
- queryParams : function (params) {
- var temp = {
- limit : params.limit, // 每页显示数量
- offset : params.offset, // SQL语句起始索引
- page: (params.offset / params.limit) + 1, //当前页码
- Name:$('#search_name').val(),
- Tel:$('#search_tel').val()
- };
- return temp;
- },
- package com.debo.common;
- public class Page {
- //每页显示数量
- private int limit;
- //页码
- private int page;
- //sql语句起始索引
- private int offset;
- public int getLimit() {
- return limit;
- }
- public void setLimit(int limit) {
- this.limit = limit;
- }
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- public int getOffset() {
- return offset;
- }
- public void setOffset(int offset) {
- this.offset = offset;
- }
- }
- package com.debo.common;
- public class User extends Page{
- private Integer id;
- private String loginName;
- private String name;
- private String tel;
- private Integer sex;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getLoginName() {
- return loginName;
- }
- public void setLoginName(String loginName) {
- this.loginName = loginName;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getTel() {
- return tel;
- }
- public void setTel(String tel) {
- this.tel = tel;
- }
- public Integer getSex() {
- return sex;
- }
- public void setSex(Integer sex) {
- this.sex = sex;
- }
- }
- package com.debo.common;
- import java.util.ArrayList;
- import java.util.List;
- public class PageHelper<T> {
- //实体类集合
- private List<T> rows = new ArrayList<T>();
- //数据总条数
- private int total;
- public PageHelper() {
- super();
- }
- public List<T> getRows() {
- return rows;
- }
- public void setRows(List<T> rows) {
- this.rows = rows;
- }
- public int getTotal() {
- return total;
- }
- public void setTotal(int total) {
- this.total = total;
- }
- }
- @RequestMapping("/getUserListPage")
- @ResponseBody
- public PageHelper<User> getUserListPage(User user,HttpServletRequest request) {
- PageHelper<User> pageHelper = new PageHelper<User>();
- // 统计总记录数
- Integer total = userService.getTotal(user);
- pageHelper.setTotal(total);
- // 查询当前页实体对象
- List<User> list = userService.getUserListPage(user);
- pageHelper.setRows(list);
- return pageHelper;
- }
- <select id="getTotal" resultType="int">
- SELECT count(1) FROM user WHERE 1 = 1
- <if test="name!=null and name !=''">
- AND name LIKE CONCAT('%',#{name},'%')
- </if>
- <if test="tel!=null and tel !=''">
- AND tel = #{tel}
- </if>
- </select>
- <select id="getUserListPage" resultType="com.debo.common.User">
- SELECT * FROM user WHERE 1 = 1
- <if test="name!=null and name !=''">
- AND name LIKE CONCAT('%',#{name},'%')
- </if>
- <if test="tel!=null and tel !=''">
- AND tel = #{tel}
- </if>
- LIMIT #{offect},#{limit}
- </select>
前端页面代码参考文章:点击打开链接
文末福利:
福利一:前端,Java,产品经理,微信小程序,Python等8G资源合集大放送:https://www.jianshu.com/p/e8197d4d9880
福利二:微信小程序入门与实战全套详细视频教程
领取方式:
如果需要学习视频,欢迎关注 【编程微刊】微信公众号,回复【领取资源】一键领取以下所有干货资源,获取更多有用技术干货、文档资料。所有文档会持续更新,欢迎关注一起成长!