This article will introduce to you two ways to implement bootstrap table paging. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Two ways of bootstrap table paging:
Front-end paging: Query all data from the database at one time and perform it on the front-end Paging (front-end paging can be used when the amount of data is small or the logic processing is not complex)
Server paging: only query the pieces of data required for loading the current page each time
bootstrap download address :http://www.bootcss.com/
bootstrap-table Download address: http://bootstrap-table.wenzhixin.net.cn/
jquery Download address: http:/ /www.jq22.com/jquery-info122
Paging effect (please ignore the style)

1: Prepare js, css and other files
Put the downloaded document directly into the webapp directory

Introduce the required js and css into the page
1 2 3 4 5 6 7 8 9 | <!-- 引入的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>
|
Copy after login
[Related recommendations : "bootstrap tutorial"】
2: HTML page tag content
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <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>
|
Copy after login
3: JS paging code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | $('#mytab').bootstrapTable({
method : 'get',
url : "user/getUserListPage" ,
striped : true,
pageNumber : 1,
pagination : true,
sidePagination : 'client',
pageSize : 4,
pageList : [ 5, 10, 20, 30 ],
showRefresh : true,
queryParams : function (params) {
var temp = {
limit : params.limit,
offset : params.offset,
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,
} ]
})
function formatSex(value, row, index) {
return value == 1 ? "男" : "女" ;
}
function operation(value, row, index) {
var htm = "<button>删除</button><button>修改</button>"
return htm;
}
$('#search_btn').click( function () {
$('#mytab').bootstrapTable('refresh', {
url : 'user/getUserListPage'
});
})
|
Copy after login
Four: bootstrap-table implements front-end paging
Modify some attributes in the JS paging code
1 2 3 4 5 6 7 8 | sidePagination:'client',
queryParams : function (params) {
var temp = {
name:$('#search_name').val(),
tel:$('#search_tel').val()
};
return temp;
},
|
Copy after login
Define user object
1 2 3 4 5 6 7 8 9 10 11 12 | package com.debo.common;
public class User {
private Integer id;
private String loginName;
private String name;
private String tel;
private Integer sex;
}
|
Copy after login
Server Controller layer code
1 2 3 4 5 6 7 8 9 | @RequestMapping( "/getUserListPage" )
@ResponseBody
public List<User> getUserListPage(User user,HttpServletRequest request){
List<User> list = userService.getUserListPage(user);
return list;
}
|
Copy after login
mabatis statement
1 2 3 4 5 6 7 8 9 | <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>
|
Copy after login
5: bootstrap-table implements server-side paging
Set certain attributes in the JS paging code
1 2 3 4 5 6 7 8 9 10 11 12 | sidePagination:'server',
queryParams : function (params) {
var temp = {
limit : params.limit,
offset : params.offset,
page: (params.offset / params.limit) + 1,
Name:$('#search_name').val(),
Tel:$('#search_tel').val()
};
return temp;
},
|
Copy after login
Encapsulate the public page object and let the user object inherit the page object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.debo.common;
public class Page {
private int limit;
private int page;
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;
}
}
|
Copy after login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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;
}
}
|
Copy after login
Encapsulate the returned data entity class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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;
}
}
|
Copy after login
Server Controller layer code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @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;
}
|
Copy after login
mybatis statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <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>
|
Copy after login
tip: Reload the table after adding, deleting, or modifying operations
1 | $( "#mytab" ).bootstrapTable('refresh', {url : url});
|
Copy after login
For more programming-related knowledge, please visit:Programming Teaching! !
The above is the detailed content of A brief discussion on two ways to implement bootstrap table paging. For more information, please follow other related articles on the PHP Chinese website!