浅谈bootstrap table分页的实现两种方式

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

浅谈bootstrap table分页的实现两种方式

本篇文章给大家介绍一下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;}}
登录后复制

封装返回数据实体类

<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>0
登录后复制

服务器Controller层代码

<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>1
登录后复制

mybatis语句

<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>2
登录后复制

tip:增、删、改操作后重新加载表格

<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>3
登录后复制

更多编程相关知识,请访问:编程教学!!

以上就是浅谈bootstrap table分页的实现两种方式的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:bootstrap是基于什么开发的?
下一篇:css伪选择器学习之伪元素选择器解析

发表评论

关闭广告
关闭广告