JavaScript实现可重用的分页查询

简要介绍

  • 分页查询解决的海量数据如何分页查询显示的问题,大量数据的查询显示必须进行分页处理
  • 本代码的分页处理由本人独立设计,耗费很多很多时间不断地改进测试,最终设计了这样一个可定制的分页处理对象,可满足很多情况下的分页处理要求

Page实现可重用的分页查询

  • 本代码设计的核心在于创建一个Page对象直接量,用于整合整合分页查询必要的属性、方法
  • 本代码实现重用的关键,在于运用当前已设计好的Page对象,覆盖其中未实现的方法,进行具体实现
  • 本代码进行移植重用,为确保功能正常,请保留所依赖的选择器的基本样式,以及Page对象中定义的属性函数,当然在熟悉本代码的情况下,可自定义进行修改
Page对象实现分页方式:
	1. 定义一个外部的点击事件函数:如initDeal()
		Page.limitpage:有默认值,设置导航栏显示的数字按钮数,int类型
		Page.limitsize:有默认值,设置每页显示记录数,int类型
		Page.arrkey:设置与结果集key一致的每一列的名字arrkey,array类型
		Page.arrexplain:设置表头文字,与arrkey顺序一致,array类型
		Page.init(param,cnt):设置查询数据库的参数、查询到的符合条件的总数量,JSON类型、int类型
		Page.initPage():只调用一次,输出表格、导航栏
	2.重新覆盖内部函数Page.classifyPageQuery():
		设置后端查询条件、起始位置、每次查询数量,JSON类型
		用返回的结果设置 Page.recordset,这样将会被Page调用

JavaScript代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#innerpagebar{
						/* min-width: 520px; */
						width:100%;
						margin: 2em auto;
						/* border:1px solid #98D2F5; */
						border-radius: 5px;
					}
					/* 导航栏按钮外观 */
					#innerpagebar table{
						text-align:center;
						margin: 0.5em auto;
						padding:0.5px 4%;
					}
					/* 导航栏按钮基本样式 */
					#innerpagebar input[type="button"]{
						width:32px;
						font-weight: normal;
						text-align:center;
						outline: none;
						padding: 2px 0px;
						border-radius: 2px;
						border: 0.2px solid #2299ff;
					}
					#innerpagebar input[name="txbtn"]{
						width:44px;
					}
					/* 导航栏按钮-鼠标停留样式 */
					#innerpagebar input[type="button"]:hover{
						background-color:#3eccff;
					}
					/* 导航栏按钮-鼠标点击样式 */
					#innerpagebar input[type="button"]:active{
						background:#98d2f5;
					}
					/* 当前按钮 */
					#innerpagebar input.currpage{
						background-color:#98d2f5;
						pointer-events: none;
					}
					/* 当前按钮-鼠标停留样式 */
					#innerpagebar input.currpage:hover {
						cursor: pointer;
						border-color: #cfcfcf;
					}
					/* 当前按钮-鼠标点击样式*/
					#innerpagebar input.currpage:active {
						background-color: #3399FF;
					}
					/* 去除样式 */
					#innerpagebar input.oldpage{
						background-color:#CFCFCF;
						pointer-events: auto;
					}
					.contab{
						width:100%;
						height:380px;
					}
					/*input按钮基本样式  */
					input.btnbase {
						font-weight: normal;
						text-align: center;
						outline: none;
						padding: 2px;
						border-radius: 2px;
						border: 0.2px solid #2299ff;
					}
					
					input.btnbase:hover {
						cursor: pointer;
						background-color: #3eccff;
						border-color: #cfcfcf;
					}
					
					input.btnbase:active {
						text-shadow: 0 1px 0 rgba(255, 255, 255, 0.3);
						background-color: #3399FF;
					}
					/* 奇偶选择器 */
						table.contab tr:nth-of-type(odd){background-color: #c3dde0;} 
						table.contab tr:nth-of-type(even){background-color: #d4e3e5;}
						table.contab {
							font-family: verdana,arial,sans-serif;
							font-size:11px;
							color:#333333;
							border-width: 1px;
							border-color: #a9c6c9;
							border-collapse: collapse;
						}
						table.contab th {
							border-width: 1px;
							padding: 5px;
							border-style: solid;
							border-color: #a9c6c9;
						}
						table.contab td {
							border-width: 1px;
							padding: 4px;
							border-style: solid;
							border-color: #a9c6c9;
						}
					
		</style>
		<script>
			// 自定义Page对象直接量
			var Page={
							// JSON对象,分页查询每次查询的结果集,每次需要设定
							recordset:"",
							//json结果集中的key
							arrkey:"",
							// json结果集的表头,必须要和arrkey一致
							arrexplain:"",
							// 显示按钮数量
							limitpage:8,
							// 每一页显示记录数量
							limitsize:10,
							// 总记录条数
							totalsize:0,
							// 总页数
							totalpage:0,
							//JSON对象,查询的参数
							param:"",
							// 导航栏第一页
							beginpage:0,
							// 导航栏最后一页
							endpage:0,
							// 当前导航按钮id
							currid:"",
							// 当前页码
							currpage:0,
							// 初始化参数,传递查询条件,符合条件的数量
							// 只进行初始化
							init:function(param,psize){
								this.param = param;
								// 总数量
								this.totalsize = psize;
								// 计算总页数
								this.totalpage = Math.ceil(this.totalsize/this.limitsize);
								if(this.totalpage>0){
									this.beginpage = 1;
									this.currpage = 1;
									this.currid = "page1";
								}
								// 确定导航栏范围
								if(this.totalpage<=this.limitpage){
									this.endpage = this.totalpage;
								}else{
									this.endpage = this.limitpage;
								}
							},
							// 首页 
							updateHomePage:function(){
								this.beginpage = 1;
								// 上一次的按钮去除样式
								this.changePageStyle("oldpage");
								//导航栏可显示的最大数字按钮,大于限制按钮数需要
								// 重新输出导航栏,翻到第一页
								if(this.endpage>this.limitpage){
									this.endpage = this.limitpage;
									this.currpage = 1;
									this.currid = "page1";
									this.printBar();
								//导航栏上的最大数字按钮在最大限制按钮数以内,
								// 无需输出导航栏,只需更新按钮样式
								}else{
									this.currpage = 1;
									this.currid = "page1";
								}
								// 当前的按钮设置样式
								this.changePageStyle("currpage");
								
							},
							
							// 点击尾页
							updateTailerPage:function(){
								// 上一次按钮去除样式
								this.changePageStyle("odlpage");
								//导航栏上当前包含尾页
								if(this.endpage==this.totalpage){
									this.currpage =this.endpage;
									var cnt = this.endpage - this.beginpage+1;
									this.currid = document.getElementById("page"+cnt).id;
								// 导航栏当前不包含尾页,说明还有下一页
								}else if(this.endpage < this.totalpage){
									this.beginpage = this.totalpage - this.limitpage+1;
									this.endpage = this.totalpage;
									this.currpage = this.totalpage;
									// 重输出导航栏
									this.printBar();
									this.currid = "page"+this.limitpage;
									// alert(this.currid);
									
								}
								// 设置当前页样式
								this.changePageStyle("currpage");
								
							},
							// 上一页更新
							updateLastPage:function(){
								
								var oldbtn = document.getElementById(this.currid);
								// 上一次的按钮去除样式
								this.changePageStyle("oldpage");
								
								// 当前页按钮不是导航栏第一个按钮,无需输出导航栏
								// 需要在当前页面更新样式
								if(this.currpage!=this.beginpage){
									var currbtn = oldbtn.parentElement.previousElementSibling.firstElementChild;
									this.currid = currbtn.id;
									this.currpage = this.currpage-1;
								// 当前页按钮如果是导航栏第一个按钮
								}else{
								// 导航栏第一个按钮前面还有上一页未显示,输出导航栏
								if(this.beginpage>this.limitpage){
									this.endpage = this.beginpage -1;
									this.beginpage = this.beginpage - this.limitpage;
									this.currpage = this.currpage-1;
									this.printBar();
									this.currid = "page"+this.limitpage;
								// 导航栏第一个按钮还有上一页,可能有不是首页,需要输出导航栏
								// 例如 页面 导航栏显示:2-11,需变换到1-10
								}else{
									this.beginpage = 1;
									this.endpage = this.limitpage; 
									this.currpage = this.currpage-1;
									this.printBar();
									this.currid = "page"+this.currpage;
								}
								}
								this.changePageStyle("currpage");
					},
					// 下一页更新
					updateNextPage:function(){
								var obj = document.getElementById(this.currid);
								// 去除上一次按钮样式
								this.changePageStyle("oldpage");
								// if(Page.currpage==1){
								// 	document.getElementById("homepage").className = "oldpage";
								// 	document.getElementById("lastpage").className = "oldpage";
								// }
								
								// // 上一次页面恢复
								// obj.className = "oldpage";
								// 导航栏当前页按钮不是当前最大数字按钮
								if(this.currpage!=this.endpage){
									var currbtn = obj.parentNode.nextElementSibling.firstChild;
									// 设置当前页按钮样式
									currbtn.className = "currpage";
									this.currid = currbtn.id;
									this.currpage += 1;
								// 导航栏当前页按钮已达到当前页最大数字按钮
								}else{
									
									// 导航栏还有下一页
									if(this.currpage<this.totalpage){
										var maxpage = this.currpage+this.limitpage;
										//输出导航栏,跳到下一页,如果能够显示完全
										if(maxpage>=this.totalpage){
											this.beginpage = this.totalpage - this.limitpage+1;
											this.endpage = this.totalpage;
											this.currpage += 1;
											this.printBar();
											var temp = this.currpage - this.beginpage+1;
											this.currid = "page"+temp;
											// document.getElementById(this.currid).className = "currpage";
										// 输出导航栏,不能够显示到尾页
										}else{
											this.beginpage = this.endpage+1;
											this.endpage = maxpage;
											this.currpage += 1;
											this.printBar();
											this.currid = "page1";
										}
										// 当前页禁用
										document.getElementById(this.currid).className = "currpage";
										
									// 恰好为尾页,没有下一页
									}
								}
								// 设置当前页样式
								this.changePageStyle("currpage");
								
							},
					// 点击数字按钮触发事件函数,不需要输出导航栏
					pageNumber:function(elembtn){
						
						// 上一次恢复
						this.changePageStyle("oldpage");
						
						// 更新
						this.currid = elembtn.id;
						
						//当前页
						Page.currpage = parseInt(elembtn.value);
						
						// 更新后
						this.changePageStyle("currpage");
						this.printTable();
					},
					// 计算查询起始位置,查询前调用
					getBeginPos:function(){
						return (this.currpage-1)*this.limitsize;
					},
					// 跳转到指定页面
					gotoPage:function(){
						
						var elembtn = document.getElementById("gotopage");
						//没有输入,返回
						if(elembtn.value==""){
							return;
						}
						var cnt = parseInt(elembtn.value);
						// 跳转页不在总页数的范围内,或者就是当前页,无需任何操作
						if(cnt<1 || cnt>this.totalpage || cnt==this.currpage){
							return;
						}
						// 需要跳转
						
						// 上次点击恢复
						this.changePageStyle("odlpage");
						// 更新
						// // 跳转的页面在导航栏上
						if(cnt>=this.beginpage && cnt<=this.endpage){
							this.currpage = cnt;
							var tempid = this.currpage - this.beginpage + 1; 
							this.currid = "page"+tempid;
						// 跳转的页面没有在导航栏上
						}else{
							// 导航栏往前翻
							if(cnt<this.beginpage){
								if(cnt<=this.limitpage){
									// alert("1");
									this.currpage = cnt;
									this.beginpage = 1;
									this.endpage = this.limitpage;
									this.printBar();
									this.currid = "page"+cnt;
								}else{
									// alert("2");
									this.endpage = cnt;
									this.beginpage = this.endpage - this.limitpage+1;
									this.currpage = cnt;
									this.printBar();
									this.currid = "page"+this.limitpage;
								}
							// 导航栏往后翻
							}else if(cnt>this.endpage){
								var tp = this.totalpage - this.limitpage;
								if(cnt>tp){
									this.beginpage = tp+1;
									this.endpage = this.totalpage;
									this.currpage = cnt;
									this.printBar();
									var sid = this.currpage-this.beginpage+1;
									this.currid = "page"+sid;
								}else{
									this.beginpage = cnt;
									this.endpage = cnt+this.limitpage-1;
									this.currpage = cnt;
									this.printBar();
									this.currid = "page1";
								}
							}
							}
							// 设置当前页样式
							this.changePageStyle("currpage");
							this.printTable();
						},
					// 文字按钮
					pageBtn:function(elembtn){
						var btnvalue = elembtn.value;
						switch(btnvalue){
							case "首页":this.updateHomePage();break;
							case "上一页":this.updateLastPage();break;
							case "下一页":this.updateNextPage();break;
							case "尾页":this.updateTailerPage();break;
						}
						this.printTable();
					},
					// 输出导航栏按钮范围
					printBar:function(){
								// 送页数为0返回
								if(this.totalpage==0){
									return;
								}
								var res = "";
								res += "<div id=\"innerpagebar\">";
								res += "<table><tr>";
								// 总页数大于2才显示首页、上一页、下一页、尾页
								if(this.totalpage>2){
								//首页按钮 
								res +="<td><input type=\"button\" name=\"txbtn\" id=\"homepage\" value=\"首页\" οnclick=\"Page.pageBtn(this)\"></td>";
								// 上一页
								res +="<td><input type=\"button\" name=\"txbtn\" id=\"lastpage\" value=\"上一页\" οnclick=\"Page.pageBtn(this)\"></td>";
								}
								// serial表示按钮上的值
								var serial = this.beginpage;
								// cnt表示id序号
								var cnt = 1;
								while(serial<=this.endpage){
									res += "<td><input type=\"button\" id=\"page"+cnt+"\" value=\""+serial+"\" οnclick=\"Page.pageNumber(this)\"></td>";
									serial += 1;
									cnt += 1;
								}
								if(this.totalpage>2){
								// 下一页
								res +="<td><input type=\"button\" name=\"txbtn\" id=\"nextpage\" value=\"下一页\" οnclick=\"Page.pageBtn(this)\"></td>";
								// 尾页
								res +="<td><input type=\"button\" name=\"txbtn\" id=\"tailerpage\" value=\"尾页\" οnclick=\"Page.pageBtn(this)\"></td>";
								res +="<td>跳转页:<input type=\"number\" id=\"gotopage\" step=\"1\" min=\"1\" value=\""+this.currpage+"\" max=\""+this.totalpage+"\"><input type=\"button\" value=\"GO\" οnclick=\"Page.gotoPage()\"></td>";
								}
								res +="<td><label>总页数:"+this.totalpage+"</label></td>";	
							res += "</tr></table>";
							
							res +="</div>";
							// pagebardiv为显示导航栏的div块
							var con = document.getElementById("pagebardiv");
							// 嵌入到div块
							con.innerHTML=res;
							
					},
					// 该方法用于输出表格:
					// 		1.内部调用classifyPageQuery()
					// 		用于访问数据库返回结果,并给recordset赋值
					// 		2.将recordset以表格形式表格输出
					printTable:function(){
						// 调用classifyPageQuery分页查询,结果保存到Page.recordset中
						this.classifyPageQuery();
						if(this.recordset==null || this.recordset==""){
							alert("结果为空");
							return;
						}
						var res = "<table class=\"contab\">";
						// 表格字段
						var headarr = Page.arrexplain;
						res += "<tr>";
						for(var i=0;i<headarr.length;i++){
							res +="<th>"+headarr[i]+"</th>";
						}
						res += "</tr>";
						//每一行结果集的key
						var headks =  this.arrkey;
						for(var i=0;i<this.recordset.length;i++){
							
							res +="<tr>";
							var item = this.recordset[i];
							for(var k=0;k<headks.length;k++){
								res += "<td>"+item[headks[k]]+"</td>";
							}
							res +="</tr>";
						}
						res += "</table>";
						document.getElementById("pageview").innerHTML=res;
						
					},
					// 翻页改变样式
					changePageStyle:function(pstyle){
						if(this.totalpage==0){return;}
						document.getElementById(this.currid).className = pstyle;
						if(this.currpage==this.totalpage && this.totalpage>2){
							document.getElementById("nextpage").className = pstyle;
							document.getElementById("tailerpage").className = pstyle;
						}
						if(this.currpage==1 && this.totalpage>2){
							document.getElementById("homepage").className = pstyle;
							document.getElementById("lastpage").className = pstyle;
						}
					},
					// 第一次查询处理:
					// 		1.在init(param,cnt)方法之后调用
					// 		2.在第一次查询数据库结果集返回结果后调用
					// 		3.内部this.printTable()调用this.classifyPageQuery()
					// 		用于查询记录,设置结果集,并以表格形式输出
					// 		4.内部this.printBar()输出导航栏
					initPage:function(){
						this.printTable();
						this.printBar();
						this.changePageStyle("currpage");
					},	
					// 定义分页查询函数,需要覆盖:
					// 		设置后端查询条件、起始位置、每次查询数量,JSON类型
					// 		用返回的结果设置 Page.recordset,这样将会被Page调用
					classifyPageQuery:function(){alert("请覆盖Page.classifyPageQuery该方法")},
					
		}
		// Page实现分页步奏:
		// 	1. 定义一个外部的点击事件函数:如initDeal()
		// 		Page.limitpage:有默认值,设置导航栏显示的数字按钮数,int类型
		// 		Page.limitsize:有默认值,设置每页显示记录数,int类型
		// 		Page.arrkey:设置与结果集key一致的每一列的名字arrkey,array类型
		// 		Page.arrexplain:设置表头文字,与arrkey顺序一致,array类型
		// 		Page.init(param,cnt):设置查询数据库的参数、查询到的符合条件的总数量,JSON类型、int类型
		// 		Page.initPage():只调用一次,输出表格、导航栏
		// 	2.重新实现内部函数Page.classifyPageQuery():
		// 		设置后端查询条件、起始位置、每次查询数量,JSON类型
		// 		用返回的结果设置 Page.recordset,这样将会被Page调用
				
		
		
		// 覆盖分页查询函数,设置查询所得的结果集,输出表格前调用
		Page.classifyPageQuery=function(){
			// Page对象中的查询参数
			var params = Page.param;
			if(params==null || params==""){
				params = {};
			}
			// 查询结果集起始位置
			params.beginpos = Page.getBeginPos();
			// 每次查询的数量
			params.eachsize = Page.limitsize;
			// params作为json对象,作为参数进行查询
			
			//Params是一个JSON对象,然后调用ajax查询
			// 这是模仿查询数据库调用
			var result = [];
			var m = params.beginpos;
			var n = params.beginpos+params.eachsize;
			while(m<n && m<Page.totalsize){
				result.push(arr[m]);
				m++;
			}
			// 设置分页查询的结果集
			Page.recordset = result;
		}
		// arr模拟数据库中确定的记录数量
		var arr=[];
		// 模拟cnt条查询记录
		function initRecordSet(cnt){
			arr = []
			var max = 180;
			var min = 155;
			for(var i=1;i<=cnt;i++){
				var obj={};
				obj.id=i;
				obj.name="A-"+i;
				obj.sex="B-"+i;
				obj.height=parseInt(Math.random()*(max-min+1)+min,10);
				arr.push(obj);
			}
			// alert(JSON.stringify(arr));
		}	
		// 首次进行查询调用
		// 	设置查询条件param,查询到的总数量,按钮数量、每页显示的数量
		function initDeal(){
			// 获取查询到的总记录数量
			var sizes = document.getElementById("pageid").value;
			if(sizes==""){
				return;
			}
			var cnt = parseInt(sizes);
			// 设置数字按钮数量
			var lsz = document.getElementById("limitpage").value;
			if(lsz!=""){
				Page.limitpage = parseInt(lsz);
			}
			// 设置每页显示的记录数量
			var recordsize = document.getElementById("limitsize").value;
			if(recordsize!=""){
				Page.limitsize = parseInt(recordsize);
			}
			//设置与结果集key一致的每一列的名字arrkey
			Page.arrkey = ["id","name","sex","height"];
			// 设置表头
			Page.arrexplain = ["编号","姓名","性别","身高"];
			
			// 初始化参数:
			// 第一个参数为查询条件,第二个参数为总记录数量
			Page.init(null,cnt);
			// 设置Page.recordset,表示第一次对结果集查询的所得的记录集
			initRecordSet(cnt);
			// 第一次查询结果集,输出导航栏和表格
			Page.initPage();
		}
		
		</script>
	</head>
	<body>
		总记录数量:<input type="number" value="104" name="pagenumber" id="pageid" min="0">
		按钮数量:<input type="number" value="11" id="limitpage" min="0">
		每页数量:<input type="number" value="8" id="limitsize" min="0">
		<button type="button" onclick="initDeal()">初始化</button>
		<!-- <button type="button" onclick="printTable()">显示表格</button> -->
		<div id="pagebardiv"></div>
		<div id="pageview"></div>
	</body>
</html>

测试效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值