简要介绍
- 分页查询解决的海量数据如何分页查询显示的问题,大量数据的查询显示必须进行分页处理
- 本代码的分页处理由本人独立设计,耗费很多很多时间不断地改进测试,最终设计了这样一个可定制的分页处理对象,可满足很多情况下的分页处理要求
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{
width:100%;
margin: 2em auto;
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.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>
var Page={
recordset:"",
arrkey:"",
arrexplain:"",
limitpage:8,
limitsize:10,
totalsize:0,
totalpage:0,
param:"",
beginpage:0,
endpage:0,
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;
}
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;
}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(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;
}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){
this.currpage = cnt;
this.beginpage = 1;
this.endpage = this.limitpage;
this.printBar();
this.currid = "page"+cnt;
}else{
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(){
if(this.totalpage==0){
return;
}
var res = "";
res += "<div id=\"innerpagebar\">";
res += "<table><tr>";
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>";
}
var serial = this.beginpage;
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>";
var con = document.getElementById("pagebardiv");
con.innerHTML=res;
},
printTable:function(){
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>";
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;
}
},
initPage:function(){
this.printTable();
this.printBar();
this.changePageStyle("currpage");
},
classifyPageQuery:function(){alert("请覆盖Page.classifyPageQuery该方法")},
}
Page.classifyPageQuery=function(){
var params = Page.param;
if(params==null || params==""){
params = {};
}
params.beginpos = Page.getBeginPos();
params.eachsize = Page.limitsize;
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;
}
var arr=[];
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);
}
}
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);
}
Page.arrkey = ["id","name","sex","height"];
Page.arrexplain = ["编号","姓名","性别","身高"];
Page.init(null,cnt);
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>
测试效果