Table of Contents
发起项目列表
Home Web Front-end Bootstrap Tutorial How to implement paging in bootstrap

How to implement paging in bootstrap

Jul 19, 2019 am 10:49 AM
bootstrap

How to implement paging in bootstrap

Recommended tutorial: BootStrap Tutorial

A common problem that everyone will face when writing a front-end The problem is paging. Pure js paging is also possible, but the amount of code may be relatively large, so today I will write an example of paging using the bootstrap framework. I hope it can help some coders who have trouble with this aspect in the future.

The first thing that needs to be clarified is which data needs to be paginated. From the data display alone, there is actually no need to paginate, because the page can be displayed, but as a relatively qualified front-end, The first thing you have to consider is not only whether this function can be implemented, but also whether the user experience is good. If you can consider more user experience issues in the existing functions, then you can be considered a relatively qualified one. front-end engineer.

1, First we will prepare the data we need (this is usually ajax request The obtained data, now we put it directly into a js, and directly take out the data when loading the js)

var testboke = {
    "code":200,
    "message":null,
    "data":{
        "total":17,//总条数
        "size":10,//分页大小-默认为0
        "pages":2,//总页数
        "current":1,//当前页数
        "records":[//author-riverLethe-double-slash-note数据部分
            {
                "id":17,//项目id
                "userName":"Night夜",//发起人名称
                "companyName":"康佰裕",//发起人公司名称
                "ptypeName":"13",//发起项目类别
                "pask":"13",
                "pname":"13",
                "pdesc":"13"
            },
            {
                "id":16,
                "userName":"Night夜",
                "companyName":"康佰裕",
                "ptypeName":"12",
                "pask":"12",
                "pname":"12",
                "pdesc":"12"
            },
            {
                "id":15,
                "userName":"BB机",
                "companyName":"北京电影",
                "ptypeName":"11",
                "pask":"11",
                "pname":"11",
                "pdesc":"11"
            },
            {
                "id":14,
                "userName":"BB机",
                "companyName":"北京电影",
                "ptypeName":"9",
                "pask":"9",
                "pname":"9",
                "pdesc":"9"
            },
            {
                "id":13,
                "userName":"BB机",
                "companyName":"北京电影",
                "ptypeName":"7",
                "pask":"7",
                "pname":"7",
                "pdesc":"7"
            },
            {
                "id":12,
                "userName":"Night夜",
                "companyName":"康佰裕",
                "ptypeName":"6",
                "pask":"6",
                "pname":"6",
                "pdesc":"6"
            },
            {
                "id":11,
                "userName":"BB机",
                "companyName":"北京电影",
                "ptypeName":"5",
                "pask":"5",
                "pname":"5",
                "pdesc":"5"
            },
            {
                "id":10,
                "userName":"Night夜",
                "companyName":"康佰裕",
                "ptypeName":"4",
                "pask":"4",
                "pname":"4",
                "pdesc":"4"
            },
            {
                "id":9,
                "userName":"BB机",
                "companyName":"北京电影",
                "ptypeName":"3",
                "pask":"3",
                "pname":"3",
                "pdesc":"3"
            },
            {
                "id":8,
                "userName":"Night夜",
                "companyName":"康佰裕",
                "ptypeName":"2",
                "pask":"2",
                "pname":"2",
                "pdesc":"2"
            }
        ]
    }
}
Copy after login

2. Next, draw the table of the page:

<body>
			<div class="templatemo-content col-1 light-gray-bg">
				<div class="templatemo-top-nav-container">
					<div class="row">
						<nav class="templatemo-top-nav col-lg-12 col-md-12">
							<li>
								<a href="">发起项目列表管理</a>
							</li>
						</nav>
					</div>
				</div>
				<!--正文部分-->
				<div style="background: #E8E8E8;height: 60rem;">
 
					<div class="templatemo-content-container">
						<div class="templatemo-content-widget no-padding">
							<!--表头-->
							<div class="panel-heading templatemo-position-relative">
								<h2 id="发起项目列表">发起项目列表</h2></div>
							<div class="panel panel-default table-responsive" id="mainContent">
 
							</div>
						</div>
					</div>
				</div>
 
				<div class="pagination-wrap" id="callBackPager">
				</div>
				<footer class="text-right">
					<p>Copyright &copy; 2018 Company Name | Designed by
						<a href="http://www.csdn.com" target="_parent">csdn</a>
					</p>
				</footer>	
	</body>
Copy after login

At this time, there are no elements on the page, because what we need is to dynamically draw the tables on the page using js, so that the retrieved data can be paginated. , but the prerequisite for drawing a table is that you must be able to get the data, right? So the next step should be to get the data instead of rushing to draw the table, because when there is no data, even if your table is drawn, it will not be displayed. , then we start to get the data:

3. We write a function to get the data:

/*将数据取出来*/
		function data(curr, limit) {
				//console.log("tot:"+totalCount)
						/*拿到总数据*/
				totalCount = testboke.data.total; //取出来的是数据总量
				dataLIst = testboke.data.records; // 将数据放到一个数组里面(dataLIst 还未声明,莫着急)
				createTable(curr, limit, totalCount);
                console.log("tot:"+totalCount)
		}
Copy after login

4. Load paging js (bootstrap paging js)

	<link href="../../css/font-awesome.min.css" rel="stylesheet" />
		<link href="../../css/bootstrap.min.css" rel="stylesheet" />
		<link href="../../css/templatemo-style.css" rel="stylesheet" />
		<link href="../../css/layui/css/layui.css" rel="stylesheet" />
		
		<script src="../../js/bootstrap.min.js" type="text/javascript"></script>
		<script src="../../js/jquery-1.8.3-min.js" type="text/javascript"></script>
		<script src="../../js/jquery.min.js" type="text/javascript"></script>
		<script src="../../js/extendPagination.js" type="text/javascript"></script>
		<script src="../../js/layui/lay/dest/layui.all.js" type="text/javascript"></script>
		<!--引如测试数据的js-->
		<script src="../../js/testcode.js" type="text/javascript"></script>
Copy after login

The above js and css are all You can find it on cdn. In addition to testcode, at the top is the js we use to load data.

5. The following is to write the paging code:

var currPage = 1;
		var totalCount;
		var dataLIst = [];
		window.onload = function() {
			/*取到总条数*/
			/*每页显示多少条  10条*/
			var limit = 10;
			data(currPage, limit)
			//console.log(totalCount)
			createTable(1, limit, totalCount);
			$(&#39;#callBackPager&#39;).extendPagination({
				totalCount: totalCount,
				limit: limit,
				callback: function(curr, limit, totalCount) {
					data(curr, limit)
				}
			});
		}
Copy after login

The effect after loading is like this:

How to implement paging in bootstrap

#6. At this time, the data has been basically processed, but the data has not been put in. Finally, we will Just put the data in, (It is not recommended to learn from my writing method. There are many ready-made methods of looping tables. I wrote it by concatenating strings natively. If you don’t mind the trouble, you can do it yourself. After all, No matter what framework it is, the bottom layer is still the implementation principle)

/*创建的是一个表格,并将数据放进去*/
		function createTable(currPage, limit, total) {
			var html = [],
				showNum = limit;
			if(total - (currPage * limit) < 0) showNum = total - ((currPage - 1) * limit);
			html.push(&#39; <table class="table table-striped table-bordered templatemo-user-table" style="margin-left: 0;">&#39;);
			html.push(&#39; <thead><tr><th>序号</th><th>项目名称</th><th>类别</th><th>发起人</th><th>单位</th><th>详情</th><th>操作</th></tr></thead><tbody>&#39;);
			for(var i = 0; i < showNum; i++) {
				html.push(&#39;<tr>&#39;);
				html.push(&#39;<td>&#39; + dataLIst[i].id + &#39;</td>&#39;);
				html.push(&#39;<td>&#39; + dataLIst[i].pname + &#39;</td>&#39;);
				html.push(&#39;<td>&#39; + dataLIst[i].ptypeName + &#39;</td>&#39;);
				html.push(&#39;<td>&#39; + dataLIst[i].userName + &#39;</td>&#39;);
				html.push(&#39;<td>&#39; + dataLIst[i].companyName + &#39;</td>&#39;);
				html.push(&#39;<td><a href="project_details_init.html?id=&#39;+dataLIst[i].id+&#39;" class="templatemo-edit-btn">详情</a></td>&#39;);
				html.push(&#39;<td><button class="templatemo-edit-btn" οnclick=checkproject(&#39; + dataLIst[i].id + &#39;,"1")>同意&#39; +
					&#39;</button> <button class="templatemo-edit-btn" οnclick=checkproject(&#39; + dataLIst[i].id + &#39;,"2")>拒绝</button></td>&#39;);
				html.push(&#39;</tr>&#39;);
			}
			html.push(&#39;</tbody></table>&#39;);
			var mainObj = $(&#39;#mainContent&#39;);
			mainObj.empty();
			mainObj.html(html.join(&#39;&#39;));
		}
Copy after login

Final rendering:

How to implement paging in bootstrap

Ok, here we have basically completed a page Loading data and paging processing are over. Isn’t it very simple? In fact, it is not difficult in the first place. It’s just that many times we don’t want to test it. Of course, the place where the data is fetched is processed by ajax, but in order to give you an example, I can only Write the part where ajax retrieves data directly into js. This data is dynamic. When using ajax to retrieve data, the paging is actually already divided when the backend gives the data. We just get the data. Come, tell him the page number, and he will give us the data corresponding to the page number. If the front end takes out all the data, is it not possible to paginate? No, it is possible, but the paging performance will be very poor, because every time you get the data is the data retrieved from the database query, which puts too much pressure on the database, we generally call this Paging is false paging.

The above is the detailed content of How to implement paging in bootstrap. 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 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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 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 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 get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

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 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.

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles