对jquery返回的json对象进行分页处理的 javascript类

//对分页资料进行处理的类
/*
options:
{
Data:数据,jquery返回的json类型资料
PageSize:每页数量
PageStart:显示的记录的起始位置
CallBack: 数据循环显示的回调函数
CallBackBefort:数据循环显示前使用的回调函数
}
AttriBute:
{
PageSize:每页数量
PageStart:起始资料
}
function:
{
SetData(数据源,是否设置起始记录位置为0):重新设置一个数据源
PageCount():当前数据源的记录总数
CallBack():
CallBackBefore():
get():处理数据,获得相关的正确的分页资料,如果实际位置小于0 则置成第一页,如果实际位置大于记录数,则设置成最后一页
Show(跳转参数):根据参数,处理分页数据,并且执行两个回调函数(0:第一页,1:上一页,2:下一页 ,其他:最后页)
}

*/
var zjPagination=function(options)
{
var options=options || [];

//把数据封装起来,不允许从外面再更新数据
var _Data=options.Data || [];
this.Data=function()
{
return _Data;
}
this.SetData=function(o,blnRefStart)
{
blnRefStart=blnRefStart||true;
_Data=o || this._Data;
if(blnRefStart)
{
this.PageStart=0;
}
}
//分页数量 默认10条记录一页
this.PageSize=options.PageSize || 10 ;
//起始记录位置
this.PageStart=options.PageStart || 0;
//记录总数
this.PageCount=function()
{
return $(_Data).size();
}
//数据循环时候的回调函数 i:数据的索引 items:对应对象
this.CallBack=options.CallBack || function(i,items){ return [];}
//在数据循环回调前执行的函数
this.CallBackBefore=options.CallBackBefore || function(){}
this.get=function()
{
var intSize=this.PageCount();

if(this.PageStart>=intSize)
{
this.PageStart=parseInt(intSize/this.PageSize)*this.PageSize;
if(this.PageStart>=intSize)
{
this.PageStart=intSize-this.PageSize;
}
}
if(this.PageStart<0)
{
this.PageStart=0;
}

var refO=new Object();
var o=$(this.Data() || []);
if(o.size()>=0){
var j=0;
for(var i=this.PageStart;i<o.size();i++)
{
if(j>=this.PageSize)
{
break;
}
refO[j]=o[i];
j++;
}
//this.PageCount=intSize;

return refO;
}else{
return [];
}
}

//获得最开始资料
this.First=function()
{
this.PageStart=0;
return this.get();
}
//获得最后一页资料
this.Last=function()
{
this.PageStart=this.PageCount();
return this.get();
}
//上翻页
this.Up=function()
{
this.PageStart=this.PageStart-this.PageSize;
return this.get();
}
//下翻页
this.Down=function()
{
this.PageStart=this.PageStart+this.PageSize;
return this.get();
}

this.Show=function(intPageType)
{
intPageType=intPageType || 0;
var o=[];
switch(intPageType)
{
case 0:
o=this.First();
break;
case 1:
o=this.Up();
break;
case 2:
o=this.Down();
break;
default:
o=this.Last();
break;
}
// o=this.get();
this.CallBackBefore();
$.each(o,this.CallBack);
}
}

使用

var thisPage=new zjPagination({
CallBack:function(a,b){ //此回调函数的写法,参考 $.each(d,function(a,b){})} 中的回调
CallBackBefore:function(){ alert('开始循环取值了'); }
});

thisPage.SetData(json);

<a href="javascript:thisPage.Show(1);">上一页</a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值