对数组分页javaScript脚本

最近一段时间写了一个脚本 对数组进行分页处理,方便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>

<script>
var ary=new Array();
for(i=0;i<21;i++)
{
    var obj=new Object();
    obj.id=i;
    obj.value="当前Id:"+i;
    ary.push(obj);
}
var ap=new Array_Page();
ap.SetArray(ary);
ap.BindFunction=function(page)
{
    var tm_ay=this.getArrayByPage(page);
    var dv_info=document.getElementById("dv_info");
    dv_info.innerHTML="";
    for(i=0;i<tm_ay.length;i++)
    {
        var obj=tm_ay[i];
        dv_info.innerHTML+="<div>Id:"+obj.id+" 内容:"+obj.value+"</div>";   
    }
    document.getElementById("lab_cpage").innerHTML=page;
    document.getElementById("lab_countPage").innerHTML=this._pageCount;    
}
ap.LastPageState=function(islast)
{
    if(!islast)
    {
        document.getElementById("a_lastPage").disabled=true;
    }else
    {document.getElementById("a_lastPage").disabled=false;}  
}
ap.NextPageState=function(isnext)
{
    if(!isnext)
    {
        document.getElementById("a_nextPage").disabled=true;
    }else
    {document.getElementById("a_nextPage").disabled=false;} 
}
ap.BindFunction(1);
</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值