search
HomeWeb Front-endBootstrap TutorialHow 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"
            }
        ]
    }
}

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>

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

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>

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

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

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
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
Using Bootstrap: Creating Modern and Mobile-First WebsitesUsing Bootstrap: Creating Modern and Mobile-First WebsitesApr 30, 2025 am 12:08 AM

Bootstrap is an open source front-end framework for creating modern, responsive, and user-friendly websites. 1) It provides grid systems and predefined styles to simplify layout and development. 2) Mobile-first design ensures compatibility and performance. 3) Through custom styles and components, the website can be personalized. 4) Performance optimization and best practices include selective loading and responsive images. 5) Common errors such as layout problems and style conflicts can be resolved through debugging techniques.

Bootstrap and Web Design: Best Practices and TechniquesBootstrap and Web Design: Best Practices and TechniquesApr 29, 2025 am 12:15 AM

Bootstrap is an open source front-end framework developed by Twitter, suitable for building responsive websites quickly. 1) Its grid system is based on a 12-column structure, allowing for the creation of flexible layouts. 2) Responsive design function enables the website to adapt to different devices. 3) The basic usage includes building a navigation bar, and the advanced usage involves card components. 4) Common errors such as misuse of grid systems can be avoided by correctly setting the column width. 5) Performance optimization includes loading only necessary components, using CDN and file compression. 6) Best practices emphasize tidy code, custom styles and responsive design.

Bootstrap and React: Combining Frameworks for Web DevelopmentBootstrap and React: Combining Frameworks for Web DevelopmentApr 28, 2025 am 12:08 AM

The reason for combining Bootstrap and React is their complementarity: 1. Bootstrap provides predefined styles and components to simplify UI design; 2. React improves efficiency and performance through component development and virtual DOM. Use it in conjunction to enjoy fast UI construction and complex interaction management.

From Zero to Bootstrap: Getting Started QuicklyFrom Zero to Bootstrap: Getting Started QuicklyApr 27, 2025 am 12:07 AM

Bootstrap is an open source front-end framework based on HTML, CSS and JavaScript, designed to help developers quickly build responsive websites. Its design philosophy is "mobile first", providing a wealth of predefined components and tools, such as grid systems, buttons, forms, navigation bars, etc., simplifying the front-end development process, improving development efficiency, and ensuring the responsiveness and consistency of the website. Using Bootstrap can start with a simple page and gradually add advanced components such as cards and modal boxes. Best practices for optimizing performance include customizing Bootstrap, using CDNs, and avoiding overuse of class names.

React and Bootstrap: Enhancing User Interface DesignReact and Bootstrap: Enhancing User Interface DesignApr 26, 2025 am 12:18 AM

React and Bootstrap can be seamlessly integrated to enhance user interface design. 1) Install dependency package: npminstallbootstrapreact-bootstrap. 2) Import the CSS file: import'bootstrap/dist/css/bootstrap.min.css'. 3) Use Bootstrap components such as buttons and navigation bars. With this combination, developers can leverage React's flexibility and Bootstrap's style library to create a beautiful and efficient user interface.

Integrating Bootstrap into React: A Practical GuideIntegrating Bootstrap into React: A Practical GuideApr 25, 2025 am 12:04 AM

The steps to integrate Bootstrap into a React project include: 1. Install the Bootstrap package, 2. Import the CSS file, 3. Use Bootstrap class name to style elements, 4. Use React-Bootstrap or reactstrap library to use Bootstrap's JavaScript components. This integration utilizes React's componentization and Bootstrap's style system to achieve efficient UI development.

What is Bootstrap Used For? A Practical ExplanationWhat is Bootstrap Used For? A Practical ExplanationApr 24, 2025 am 12:16 AM

Bootstrapisapowerfulframeworkthatsimplifiescreatingresponsive,mobile-firstwebsites.Itoffers:1)agridsystemforadaptablelayouts,2)pre-styledelementslikebuttonsandforms,and3)JavaScriptcomponentssuchascarouselsforenhancedinteractivity.

Bootstrap: From Layouts to ComponentsBootstrap: From Layouts to ComponentsApr 23, 2025 am 12:06 AM

Bootstrap is a front-end framework developed by Twitter that integrates HTML, CSS and JavaScript to help developers quickly build responsive websites. Its core functions include: Grid system and layout: based on 12-column design, using flexbox layout, and supporting responsive pages of different device sizes. Components and styles: Provide a rich library of component, such as buttons, modal boxes, etc., and you can achieve beautiful effects by adding class names. How it works: Rely on CSS and JavaScript, CSS uses LESS or SASS preprocessors, and JavaScript relies on jQuery to achieve interactive and dynamic effects. Through these features, Bootstrap greatly improves development

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),