JS分页类

/**
* filename: page.js
function showpage(nowpage){
page.construct(1000,20,'jspage.php?page=',nowpage);
document.write(page.show(1));
}
开启AJAX:
function ajaxpage(nowpage){
page.construct(1000,20,'ajax.php?page=',nowpage,true,'ajax_page');
document.write(page.show(1));
}
采用继承自定义分页显示模式:
function mypage(nowpage){
page.construct(1000,20,'jspage.php?page=',nowpage);
page._next_page='>‘;
page._pre_page=’<';
page._first_page='<<';
page._last_page='>>’;
page._is_ajax=false;
document.write(page._first()+’ ‘+page._pre()+’ ‘+page._next()+’ ‘+page._last());
}
*/
var page={
_pagebarnum : 10,
_totalpage : 1,
_nowpage : 1,
_pageurl : ”,//eg. ‘/index.php?pagename=’ or ‘/index.php?id=1&pagename=’
_is_ajax : false,
_ajax_page : ”,

/**setting*/
_next_page : ‘>’,
_pre_page : ‘<',
_first_page : '首页',
_last_page : '尾页',
_text_left : '[',
_text_right : ']',
_pagebarnum : 10,

/**
* 构造函数
*/
construct:function(total,perpage,pageurl,nowpage,is_ajax,ajax_page){
this._pageurl=pageurl;
if(nowpage!=null && nowpage!='')this._nowpage=parseInt(nowpage);
this._totalpage=Math.ceil(total/perpage);
if(is_ajax!=null && is_ajax!=''){
this._is_ajax=is_ajax;
this._ajax_page=ajax_page;
}
},
/**
* 获取下一页
*/
_next:function(style){
if(this._nowpage return this._getlink(this._nowpage+1,this._next_page,this._getstyle(style));
}
return '‘+this._next_page+’‘;
},
/**
* 获取上一页
*/
_pre:function(style){
if(this._nowpage>1){
return this._getlink(this._nowpage-1,this._pre_page,this._getstyle(style));
}
return ‘‘+this._pre_page+’‘;
},
/**
* 获取第一页
*/
_first:function(style){
if(this._nowpage!=1){
return this._getlink(1,this._first_page,this._getstyle(style));
}
return ‘‘+this._first_page+’‘;
},
/**
* 获取最后一页
*/
_last:function(style){
if(this._nowpage!=this._totalpage){
return this._getlink(this._totalpage,this._last_page,this._getstyle(style));
}
return ‘‘+this._last_page+’‘;
},
/**
* 获取显示的分页栏,默认当前页面在最前面
* @param style 未选中的风格
* @param nowstyle 当前页面的风格
*/
_pagebar:function(style,nowstyle){
var plus=Math.ceil(this._pagebarnum/2);
if(this._pagebarnum-plus+this._nowpage>this._totalpage)plus=this._nowpage+this._pagebarnum-this._totalpage;
var begin=this._nowpage-plus+1;
if(begin<1)begin=1;
var returnstr='';
for(i=begin;i if(i<=this._totalpage){
if(i==this._nowpage){
returnstr+=this._text_left+'‘+i+’‘+this._text_right;
}else{
returnstr+=this._text_left+this._getlink(i,i,this._getstyle(style))+this._text_right;
}
}else{
break;
}
}
return returnstr;
},
/**
* 显示下拉列表
*/
_select:function(){
var returnstr=’

‘;
},

/**
* 返回结果
*/
show:function(mode){
if((mode==null)||(mode==”))mode=1;
switch(mode)
{
case 1:
this._pre_page=’上一页’;
this._next_page=’下一页’;
return this._pre()+this._pagebar()+this._next()+this._select();
case 2:
this._pre_page=’上一页’;
this._next_page=’下一页’;
this._first_page=’首页’;
this._last_page=’尾页’;
return this._first()+this._pre()+’[第'+this._nowpage+'页]‘+this._next()+this._last()+’第’+this._select()+’页’;
case 3:
this._pre_page=’上一页’;
this._next_page=’下一页’;
return this._first()+’ ‘+this._pre()+this._next()+’ ‘+this._last()+this._select();
case 4:
this._pre_page=’上一页’;
this._next_page=’下一页’;
return this._first()+’ ‘+this._pre()+this._pagebar()+this._next()+’ ‘+this._last()+this._select();
}
},
/**private—–*/
/**
* 获取页面的链接
* @pageno 页面值。第几页
* @pagename 页面显示的文字
* @style 显示的风格
* @return 分页链接
*/
_getlink:function(pageno,pagename,style){
if((pagename==null)||(pagename==”))pagename=pageno
if(this._is_ajax){
//ajax模式
return ‘‘+pagename+’‘;
}else{
//一般模式
return ‘‘+pagename+’‘;
}
},
/**
* 获取风格
*/
_getstyle:function(style){
if(style!=null && style!=”){
style=’ class=”‘+style+’”‘;
}else{
style=”;
}
return style;
}
}
?>

留言