bootstrap table分頁的兩種方式:javascript
前端分頁:一次性從數據庫查詢全部的數據,在前端進行分頁(數據量小的時候或者邏輯處理不復雜的話能夠使用前端分頁)css
服務器分頁:每次只查詢當前頁面加載所須要的那幾條數據html
bootstrap 下載地址:bootstrap下載前端
bootstrap-table 下載地址:bootstrap-table下載java
jquery下載地址:jquery下載jquery
分頁效果(請忽略樣式)web
一:準備js、css等文件sql
▶ 將下載的文檔直接放入webapp目錄下數據庫
▶頁面引入須要的js、cssbootstrap
-
-
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" />
-
<link href="bootstrap-table/dist/bootstrap-table.min.css"
-
-
-
<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>
二:html頁面標籤內容
-
<div class="panel panel-default">
-
<div class="panel-heading">
-
-
-
<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>
-
-
<input type="text" class="form-control" name="Name" id="search_name"/>
-
-
<label class="col-sm-1 control-label" style="text-align: right; margin-top:5px">手機號:</label>
-
-
<input type="text" class="form-control" name="Name" id="search_tel"/>
-
-
<div class="col-sm-1 col-sm-offset-4">
-
<button class="btn btn-primary" id="search_btn">查詢</button>
-
-
-
-
<table id="mytab" class="table table-hover"></table>
三:JS分頁代碼
-
$('#mytab').bootstrapTable({
-
-
url : "user/getUserListPage",//請求路徑
-
striped : true, //是否顯示行間隔色
-
pageNumber : 1, //初始化加載第一頁
-
-
sidePagination : 'client',//server:服務器端分頁|client:前端分頁
-
-
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()
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
formatter : formatSex,//對返回的數據進行處理再顯示
-
-
-
-
formatter : operation,//對資源進行操做
-
-
-
-
-
function formatSex(value, row, index) {
-
return value == 1 ? "男" : "女";
-
//或者 return row.sex == 1 ? "男" : "女";
-
-
-
-
function operation(value, row, index) {
-
var htm = "
<button>刪除</button><button>修改</button>"
-
-
-
-
-
$('#search_btn').click(function() {
-
$('#mytab').bootstrapTable('refresh', {
-
url : 'user/getUserListPage'
-
-
四:bootstrap-table 實現前端分頁
▶ 修改JS分頁代碼中某些屬性
-
-
queryParams : function (params) {
-
-
name:$('#search_name').val(),
-
tel:$('#search_tel').val()
-
-
-
▶ 定義user對象
-
-
-
-
-
-
private String loginName;
-
-
-
-
-
-
-
public void setId(Integer id) {
-
-
-
public String getLoginName() {
-
-
-
public void setLoginName(String loginName) {
-
this.loginName = loginName;
-
-
public String getName() {
-
-
-
public void setName(String name) {
-
-
-
-
-
-
public void setTel(String tel) {
-
-
-
public Integer getSex() {
-
-
-
public void setSex(Integer sex) {
-
-
-
▶ 服務器Controller層代碼
-
-
*直接一次性查出全部的數據,返回給前端,bootstrap-table自行分頁
-
-
@RequestMapping("/getUserListPage")
-
-
public List
<User> getUserListPage(User user,HttpServletRequest request){
-
List
<User> list = userService.getUserListPage(user);
-
-
▶ 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 test="tel!=null and tel !=''">
-
-
-
五:bootstrap-table 實現服務器端分頁
▶ 設置JS分頁代碼中的某些屬性
-
-
queryParams : function (params) {
-
-
limit : params.limit, // 每頁顯示數量
-
offset : params.offset, // SQL語句起始索引
-
page: (params.offset / params.limit) + 1, //當前頁碼
-
-
Name:$('#search_name').val(),
-
Tel:$('#search_tel').val()
-
-
-
▶ 封裝公共的page對象,並讓user對象繼承page對象
-
-
-
-
-
-
-
-
-
-
-
-
-
public void setLimit(int limit) {
-
-
-
-
-
-
public void setPage(int page) {
-
-
-
-
-
-
public void setOffset(int offset) {
-
-
-
-
-
-
-
public class User extends Page{
-
-
-
private String loginName;
-
-
-
-
-
-
-
public void setId(Integer id) {
-
-
-
public String getLoginName() {
-
-
-
public void setLoginName(String loginName) {
-
this.loginName = loginName;
-
-
public String getName() {
-
-
-
public void setName(String name) {
-
-
-
-
-
-
public void setTel(String tel) {
-
-
-
public Integer getSex() {
-
-
-
public void setSex(Integer sex) {
-
-
-
▶ 封裝返回數據實體類
-
-
-
import java.util.ArrayList;
-
-
-
public class PageHelper
<T> {
-
-
private List
<T> rows = new ArrayList<T>();
-
-
-
-
-
-
-
-
public List
<T> getRows() {
-
-
-
-
public void setRows(List
<T> rows) {
-
-
-
-
-
-
-
-
public void setTotal(int total) {
-
-
-
-
▶ 服務器Controller層代碼
-
@RequestMapping("/getUserListPage")
-
-
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);
-
-
-
▶ 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 test="tel!=null and tel !=''">
-
-
-
-
-
<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 test="tel!=null and tel !=''">
-
-
-
-
tip:增、刪、改操做後從新加載表格
$("#mytab").bootstrapTable('refresh', {url : url});