最近一段时间写了一个脚本 对数组进行分页处理,方便AJAX异步处理.分享给大家
封装了一下,参数大家一眼就能看出来啥意思
function Array_Page()
{
this._pageSize=10;
this._pageCount=0;
this._arrayCount=0;
this._currentPage=1;
this.IsNextPage=true;
this.IsLastPage=true;
var _tmeArray=new Array();
this.PushItem=function(item)//添加项
{
_tmeArray.push(item);
this.SetDefValue();
}
this.SetArray=function(array)//直接设定数组
{
_tmeArray=array;
this.SetDefValue();
}
this.getArrayByPage=function(page)
{
if(page<=1)
{
page=1;
this._currentPage=1;
this.IsLastPage=false;
}
else
{
this.IsLastPage=true;
}
if(page>=this._pageCount)
{
page=this._pageCount;
this._currentPage=this._pageCount;
this.IsNextPage=false;
}
else
{this.IsNextPage=true;}
this._currentPage=page;
this.LastPageState(this.IsLastPage);
this.NextPageState(this.IsNextPage);
var fromi =((page-1)*this._pageSize);
var toi=fromi+this._pageSize;
var tmpArrat=new Array();
if(toi>_tmeArray.length)
{ toi=_tmeArray.length; }
for(i=fromi;i<toi;i++)
{
tmpArrat.push(_tmeArray[i]);
}
return tmpArrat;
}
this.NextPage=function()
{
this.BindFunction(this._currentPage+1);
}
this.LastPage=function()
{
this.BindFunction(this._currentPage-1);
}
this.SetDefValue=function()
{
this._arrayCount=_tmeArray.length;
this._pageCount=this._arrayCount % this._pageSize > 0 ? parseInt(this._arrayCount / this._pageSize) + 1 : parseInt(this._arrayCount / this._pageSize);
}
//数据绑定方法可对页面进行设置
this.BindFunction=function(page)
{}
//达到最后一页
this.LastPageState=function(islast)
{}
//达到第一页
this.NextPageState=function(isnext)
{}
}
调用以及使用方法
<div id="dv_info"></div>
<div id="dv_page">当前 <label id="lab_cpage"></label>页 总共<label id="lab_countPage"></label> <a href="JavaScript:ap.LastPage();" id="a_lastPage">上页</a> <a href="JavaScript:ap.NextPage();" id="a_nextPage">下页</a></div>

1115

被折叠的 条评论
为什么被折叠?



