前几天有朋友问给定一个数,用它来构造一个2维数组,当时想用它来在页面管理页码倒不错!按宁增勿改的原则:
1:在原JS代码中加几个了全局变量,最终的全局变量列表为:
<
script
type
="text/javascript"
>
...
//<![CDATA[
var globalLink;
var pageArray;
var currentPage;
var totalPage;
//<![CDATA[
var globalLink;
var pageArray;
var currentPage;
var totalPage;
原来的两个函数不变,再加几个函数:
function pageStruct(paran,e){
var one=0;
var two=0;
var page=new Array();
if(param.length==0){
alert("error");
}else{
totalPage=param;
page[one]=new Array();
for(var i=0;i < param ;i++){
page[one][two] =i+1;
two++;
if(two ==5) two =0;
if((i+1)/5 > =1 && (i+1)%5==0){
one++;
page[one]=new Array();
}
}
}
pageArray=page;
currentPage=e;
if(currentPage < =0 || currentPage ==undefined) currentPage =1;
pagePanel(findPageArray());
}
var one=0;
var two=0;
var page=new Array();
if(param.length==0){
alert("error");
}else{
totalPage=param;
page[one]=new Array();
for(var i=0;i < param ;i++){
page[one][two] =i+1;
two++;
if(two ==5) two =0;
if((i+1)/5 > =1 && (i+1)%5==0){
one++;
page[one]=new Array();
}
}
}
pageArray=page;
currentPage=e;
if(currentPage < =0 || currentPage ==undefined) currentPage =1;
pagePanel(findPageArray());
}
该函数的作用:
a,用param参数构造一个2维数组,并把该数组赋给全局变量:pageArray;
b,用e参数给全局变量:currentPage赋值
c,打印页面面板
看另一个函数:
function findPageArray(){
var tempParam;
for(var i=0;i < pageArray .length;i++){
for(var j =0;j<pageArray[i].length;j++){
if(pageArray[i][j] ==currentPage){
tempParam =i;
break;
}
}
}
return tempParam;
}
var tempParam;
for(var i=0;i < pageArray .length;i++){
for(var j =0;j<pageArray[i].length;j++){
if(pageArray[i][j] ==currentPage){
tempParam =i;
break;
}
}
}
return tempParam;
}
该函数的作用:
a.用currentPage来定位2维数组
最后一个函数:
function pagePanel(param){
if(param.length==0 || param < =0 ) param =0;
var tempPrev =currentPage-1;
if(tempPrev< =0) tempPrev =1;
var tempNext =currentPage+1;
if(tempNext > =totalPage) totalNext=totalPage;
var lastPos=pageArray[param].length-1;
pageLink=" < dl >< dt > Results Page: </ dt > ";
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+pageArray[param][0]+"));' > Index </ a ></ dd > ";
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+tempPrev+"));' > Prev </ a ></ dd > ";
for(var i=0;i < pageArray [param].length;i++){
pageLink =pageLink+"<dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+pageArray[param][i]+"));' > "+pageArray[param][i]+" </ a ></ dd > ";
}
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+tempNext+"));' > Next </ a ></ dd > ";
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+pageArray[param][lastPos]+"));' > Last </ a ></ dd ></ dl > ";
document.getElementById("pagelist").innerHTML=pageLink;
}
if(param.length==0 || param < =0 ) param =0;
var tempPrev =currentPage-1;
if(tempPrev< =0) tempPrev =1;
var tempNext =currentPage+1;
if(tempNext > =totalPage) totalNext=totalPage;
var lastPos=pageArray[param].length-1;
pageLink=" < dl >< dt > Results Page: </ dt > ";
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+pageArray[param][0]+"));' > Index </ a ></ dd > ";
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+tempPrev+"));' > Prev </ a ></ dd > ";
for(var i=0;i < pageArray [param].length;i++){
pageLink =pageLink+"<dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+pageArray[param][i]+"));' > "+pageArray[param][i]+" </ a ></ dd > ";
}
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+tempNext+"));' > Next </ a ></ dd > ";
pageLink=pageLink+" < dd >< a href ='javascript:window.location.replace(getRequestPageNumber("+pageArray[param][lastPos]+"));' > Last </ a ></ dd ></ dl > ";
document.getElementById("pagelist").innerHTML=pageLink;
}
该函数的作用:
a.用findPageArray函数返回的数数组作引元打印页面面板
最后网页的改动为:
<
div
id
="pagepanel"
>
< label id ="pagelist" ></ label >
< script type ="text/javascript" language ="javascript" > ... pageStruct(<%=p.getTotalPages()%>,<%=p.getCurrentPage() %>) </ script >
< p > <% = p.getCurrentPage() %> / <% = p.getTotalPages() %> </ p >
</ div >
< label id ="pagelist" ></ label >
< script type ="text/javascript" language ="javascript" > ... pageStruct(<%=p.getTotalPages()%>,<%=p.getCurrentPage() %>) </ script >
< p > <% = p.getCurrentPage() %> / <% = p.getTotalPages() %> </ p >
</ div >
其实用单例的模式把JS重构为面向对象的写法更好!可以避免重复的在页面构造2维数组