Home Web Front-end Bootstrap Tutorial A brief discussion on the two ways to implement table paging in bootstrap (front-end/server)

A brief discussion on the two ways to implement table paging in bootstrap (front-end/server)

Feb 01, 2021 pm 05:27 PM
bootstrap table Pagination sheet

This article will introduce to you bootstrap the two front-end and back-end implementation methods of table paging. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on the two ways to implement table paging in bootstrap (front-end/server)

Related recommendations: "bootstrap tutorial"

Two ways of paginating bootstrap table:

Front-end paging: Query all data from the database at one time and perform paging on the front-end (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

▶ Place the downloaded documents directly into the webapp directory

▶The page introduces the required js and 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>
Copy after login

2: HTML page tag content

<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

$(&#39;#mytab&#39;).bootstrapTable({
	method : &#39;get&#39;,
	url : "user/getUserListPage",//请求路径
	striped : true, //是否显示行间隔色
	pageNumber : 1, //初始化加载第一页
	pagination : true,//是否分页
	sidePagination : &#39;client&#39;,//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 : $(&#39;#search_name&#39;).val(),
			Tel : $(&#39;#search_tel&#39;).val()
		};
		return temp;
	},
	columns : [ {
		title : &#39;登录名&#39;,
		field : &#39;loginName&#39;,
		sortable : true
	}, {
		title : &#39;姓名&#39;,
		field : &#39;name&#39;,
		sortable : true
	}, {
		title : &#39;手机号&#39;,
		field : &#39;tel&#39;,
	}, {
		title : &#39;性别&#39;,
		field : &#39;sex&#39;,
		formatter : formatSex,//对返回的数据进行处理再显示
	}, {
		title : &#39;操作&#39;,
		field : &#39;id&#39;,
		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;
}
 
//查询按钮事件
$(&#39;#search_btn&#39;).click(function() {
	$(&#39;#mytab&#39;).bootstrapTable(&#39;refresh&#39;, {
		url : &#39;user/getUserListPage&#39;
	});
})
Copy after login

4: bootstrap-table implements front-end paging

▶ Modify some attributes in the JS paging code

sidePagination:&#39;client&#39;,
queryParams : function (params) {
        var temp = {
            name:$(&#39;#search_name&#39;).val(),
            tel:$(&#39;#search_tel&#39;).val()
        };
        return temp;
    },
Copy after login

▶ Define user Object

package com.debo.common;

public class User {
	
	private Integer id;
	private String loginName;
	private String name;
	private String tel;
	private Integer sex;
	
        //省略Get/Set函数
}
Copy after login

▶ Server Controller layer code

/**
*直接一次性查出所有的数据,返回给前端,bootstrap-table自行分页
*/
@RequestMapping("/getUserListPage")
@ResponseBody
public List<User> getUserListPage(User user,HttpServletRequest request){
	List<User> list = userService.getUserListPage(user);
	return list;
}
Copy after login

▶ mabatis statement

<select id="getUserListPage" resultType="com.debo.common.User">
	SELECT * FROM user WHERE 1 = 1
	<if test="name!=null and name !=&#39;&#39;">
		AND name LIKE CONCAT(&#39;%&#39;,#{name},&#39;%&#39;)
	</if>
	<if test="tel!=null and tel !=&#39;&#39;">
		AND tel = #{tel}
	</if>
</select>
Copy after login

5: bootstrap-table implements server-side paging

▶ Set some properties in the JS paging code

sidePagination:&#39;server&#39;,
queryParams : function (params) {
    var temp = {
        limit : params.limit, // 每页显示数量
        offset : params.offset, // SQL语句起始索引
        page: (params.offset / params.limit) + 1,   //当前页码
            
        Name:$(&#39;#search_name&#39;).val(),
        Tel:$(&#39;#search_tel&#39;).val()
    };
    return temp;
},
Copy after login

▶ Encapsulate the public page object and let the user object inherit the page object

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;
	}

}
Copy after login
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

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

@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

<select id="getTotal" resultType="int">
	SELECT count(1) FROM user WHERE 1 = 1
	<if test="name!=null and name !=&#39;&#39;">
		AND name LIKE CONCAT(&#39;%&#39;,#{name},&#39;%&#39;)
	</if>
	<if test="tel!=null and tel !=&#39;&#39;">
		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 !=&#39;&#39;">
		AND name LIKE CONCAT(&#39;%&#39;,#{name},&#39;%&#39;)
	</if>
	<if test="tel!=null and tel !=&#39;&#39;">
		AND tel = #{tel}
	</if>
	LIMIT #{offset},#{limit}
</select>
Copy after login

tip: Reload the table after adding, deleting or modifying operations

$("#mytab").bootstrapTable(&#39;refresh&#39;, {url : url});
Copy after login

Update For multi-computer programming related knowledge, please visit: programming video! !

The above is the detailed content of A brief discussion on the two ways to implement table paging in bootstrap (front-end/server). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

How to change the size of a Bootstrap list? How to change the size of a Bootstrap list? Apr 07, 2025 am 10:45 AM

The size of a Bootstrap list depends on the size of the container that contains the list, not the list itself. Using Bootstrap's grid system or Flexbox can control the size of the container, thereby indirectly resizing the list items.

How to write a carousel picture on bootstrap How to write a carousel picture on bootstrap Apr 07, 2025 pm 12:54 PM

Creating a carousel chart using Bootstrap requires the following steps: Create a container containing a carousel chart, using the carousel class. Add a carousel image to the container, using the carousel-item class and the active class (only for the first image). Add control buttons, using the carousel-control-prev and carousel-control-next classes. Add a carousel-indicators metric (small dots), using the carousel-indicators class (optional). Set up automatic playback and add data-bs-ride="carousel&"on the carousel" container.

How to verify bootstrap date How to verify bootstrap date Apr 07, 2025 pm 03:06 PM

To verify dates in Bootstrap, follow these steps: Introduce the required scripts and styles; initialize the date selector component; set the data-bv-date attribute to enable verification; configure verification rules (such as date formats, error messages, etc.); integrate the Bootstrap verification framework and automatically verify date input when form is submitted.

See all articles