Home >Web Front-end >Bootstrap Tutorial >How to implement paging in bootstrap

How to implement paging in bootstrap

angryTom
angryTomOriginal
2019-07-19 10:49:194887browse

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 class="text-uppercase">发起项目列表</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