本篇文章给大家介绍一下bootstrap table分页的实现两种方式。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
bootstrap table分页的两种方式:
前端分页:一次性从数据库查询所有的数据,在前端进行分页(数据量小的时候或者逻辑处理不复杂的话可以使用前端分页)
服务器分页:每次只查询当前页面加载所需要的那几条数据
bootstrap 下载地址:http://www.bootcss.com/
bootstrap-table 下载地址:http://bootstrap-table.wenzhixin.net.cn/
jquery下载地址:http://www.jq22.com/jquery-info122
分页效果(请忽略样式)
一:准备js、css等文件
将下载的文档直接放入webapp目录下
页面引入需要的js、css
<!-- 引入的css文件 --><link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" /><link href="bootstrap-table/dist/bootstrap-table.min.css" rel="stylesheet"><!-- 引入的js文件 --><script src="jquery/jquery.min.js"></script><script src="bootstrap/js/bootstrap.min.js"></script><script src="bootstrap-table/dist/bootstrap-table.min.js"></script><script src="bootstrap-table/dist/locale/bootstrap-table-zh-cn.min.js"></script>
【相关推荐:《bootstrap教程》】
二:html页面标签内容
<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>
三:js分页代码
$('#mytab').bootstraptable({ method : 'get', url : "user/getuserlistpage",//请求路径 striped : true, //是否显示行间隔色 pagenumber : 1, //初始化加载第一页 pagination : true,//是否分页 sidepagination : 'client',//server:服务器端分页|client:前端分页 pagesize : 4,//单页记录数 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,//对返回的数据进行处理再显示 }, { title : '操作', field : 'id', formatter : operation,//对资源进行操作 } ]}) //value代表该列的值,row代表当前对象function formatsex(value, row, index) { return value == 1 ? "男" : "女"; //或者 return row.sex == 1 ? "男" : "女";} //删除、编辑操作function operation(value, row, index) { var htm = "<button>删除</button><button>修改</button>" return htm;} //查询按钮事件$('#search_btn').click(function() { $('#mytab').bootstraptable('refresh', { url : 'user/getuserlistpage' });})
四:bootstrap-table 实现前端分页
修改js分页代码中某些属性
sidepagination:'client',queryparams : function (params) { var temp = { name:$('#search_name').val(), tel:$('#search_tel').val() }; return temp; },
定义user对象
package com.debo.common; public class user { private integer id; private string loginname; private string name; private string tel; private integer sex; //省略get/set函数}
服务器controller层代码
/***直接一次性查出所有的数据,返回给前端,bootstrap-table自行分页*/@requestmapping("/getuserlistpage")@responsebodypublic list<user> getuserlistpage(user user,httpservletrequest request){ list<user> list = userservice.getuserlistpage(user); return list;}
mabatis语句
<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>
五:bootstrap-table 实现服务器端分页
设置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;},
封装公共的page对象,并让user对象继承page对象
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; } }
服务器controller层代码
@requestmapping("/getuserlistpage")@responsebodypublic 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;}
mybatis语句
<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 #{offset},#{limit}</select>
tip:增、删、改操作后重新加载表格
$("#mytab").bootstraptable('refresh', {url : url});
更多编程相关知识,请访问:编程教学!!
以上就是浅谈bootstrap table分页的实现两种方式的详细内容。