首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

面向对象的纯js分页组件,该如何处理

2013-08-09 
面向对象的纯js分页组件本帖最后由 cj205 于 2010-12-23 16:53:30 编辑这是小弟我开发的一个纯js的分页组

面向对象的纯js分页组件
本帖最后由 cj205 于 2010-12-23 16:53:30 编辑 这是小弟我开发的一个纯js的分页组件,支持刷新的和不刷新的分页,和任何后台技术无关,完全用js来控制,因为里面的代码依赖了jquery,所以使用时要先导入jquery.js。

代码如下:

/**
 * 
 * @param {} recordCount  总的记录数
 * @param {} pageSize     每页的记录数 
 * @param {} divId        容纳分页元素的id
 * @param {} funPageSearch  回调的查询方法
 * currentPage  当前页,默认为1
 * @author:yxd
 * @email:yxd_yxd2008@163.com
 */
function Pager(recordCount,pageSize,divId,funPageSearch,currentPage){
this.recordCount=recordCount;
this.pageSize=pageSize;
this.pageContainer=$("#"+divId);  
this.funPageSearch=funPageSearch;
this.currentPage=currentPage || 1;
this.totalPage=this.calculateTotalPage(recordCount);
        this.htmlRecordCount=null;
        this.htmlTotalPage=null;
        this.htmlInputPage=null;
        this.htmlFirstPage=null;
        this.htmlPrvPage=null;
        this.htmlNextPage=null;
        this.htmlLastPage=null;
        this.htmlDiv=null;
this.draw();
}

Pager.prototype={
draw:function(){
var oo=this;
var str='共<span>'+this.recordCount+'</span>行&nbsp;&nbsp;&nbsp;'+
        '每页'+this.pageSize+'行&nbsp;&nbsp;&nbsp;'+       
        '共<span>'+this.totalPage+'</span>页&nbsp;&nbsp;&nbsp;'+
    '<div style="display:none;">转到第<input value="'+this.currentPage+'" style="width:28px" maxlength="'+this.maxLength()+'">页&nbsp;&nbsp;&nbsp;'+
         '<span>首页</span>&nbsp;&nbsp;&nbsp;'+
         '<span>上页</span>&nbsp;&nbsp;&nbsp;'+
         '<span>下页</span>&nbsp;&nbsp;&nbsp;'+
         '<span>尾页</span></div>&nbsp;&nbsp;&nbsp;';
this.pageContainer.append(str);
this.htmlRecordCount=$("span:eq(0)",this.pageContainer);


this.htmlTotalPage=$("span:eq(1)",this.pageContainer);
this.htmlInputPage=$("input:eq(0)",this.pageContainer);
this.htmlFirstPage=$("span:eq(2)",this.pageContainer);
this.htmlPrvPage=$("span:eq(3)",this.pageContainer);
this.htmlNextPage=$("span:eq(4)",this.pageContainer);
this.htmlLastPage=$("span:eq(5)",this.pageContainer);
this.htmlDiv=$("div",this.pageContainer);
            if(this.totalPage>1){
            this.htmlDiv.css("display","inline");            
            }
            //设定按钮的样式
            this.setButtonStyle();
            
            //为按钮绑定事件
            //首页
            this.htmlFirstPage.click(function(){
            if(oo.currentPage!=1){
            oo.currentPage=1;
            oo.gotoPage();
            }
            });
            
            //上页
            this.htmlPrvPage.click(function(){
            if(oo.currentPage!=1){
            oo.currentPage--;
            oo.gotoPage();
            }
            });
            
            //下页
            this.htmlNextPage.click(function(){
            if(oo.currentPage!=oo.totalPage){
            oo.currentPage++;
            oo.gotoPage();


            }
            });
            
            //尾页
            this.htmlLastPage.click(function(){
            if(oo.currentPage!=oo.totalPage){
            oo.currentPage=oo.totalPage;
            oo.gotoPage();
            }
            });
            
            //处理输入框的事件
            this.htmlInputPage.focus(function(){
            this.select();
            }).change(function(){
            if(   isNaN(Number(this.value)) 
               || Number(this.value)==0 
               || Number(this.value)>oo.totalPage){
            this.value=oo.currentPage;
            return;
            }
            oo.currentPage=Number(this.value);
            oo.gotoPage();            
            }).keydown(function(evt){
            var validKey={48:0,49:1,50:2,51:3,52:4,53:5,54:6,55:7,56:8,57:9,
                      96:0,97:1,98:2,99:3,100:4,101:5,102:6,103:7,104:8,105:9,
                      8:'',37:'',39:'',46:''};
if(!(evt.keyCode in validKey)){
return false;
}


            }); 
},
gotoPage:function(){
if(this.htmlInputPage.val()!=this.currentPage){
this.htmlInputPage.val(this.currentPage);
}
this.setButtonStyle();
this.funPageSearch(this.currentPage);
},
maxLength:function(){//计算输入页数的长度
return (this.totalPage+'').length;
},
setButtonStyle:function(){//根据当前的页数设置按钮的样式
switch(this.currentPage){
case 1:{
this.htmlFirstPage.css({cursor:"",color:"grey"});
this.htmlPrvPage.css({cursor:"",color:"grey"});
this.htmlNextPage.css({cursor:"pointer",color:"black"});
this.htmlLastPage.css({cursor:"pointer",color:"black"});
break;
}
case this.totalPage:{
this.htmlFirstPage.css({cursor:"pointer",color:"black"});
this.htmlPrvPage.css({cursor:"pointer",color:"black"});
this.htmlNextPage.css({cursor:"",color:"grey"});
this.htmlLastPage.css({cursor:"",color:"grey"});
break;
}
default:{
this.htmlFirstPage.css({cursor:"pointer",color:"black"});
this.htmlPrvPage.css({cursor:"pointer",color:"black"});
this.htmlNextPage.css({cursor:"pointer",color:"black"});
this.htmlLastPage.css({cursor:"pointer",color:"black"});
break;
}
}
},
calculateTotalPage:function(_recordCount){//计算出总页数
return Math.floor((_recordCount+this.pageSize-1)/this.pageSize);
},
reDraw:function(_recordCount){//对于不刷新的查询,需要根据总记录数来重新设置分页元素
this.currentPage=1;
this.recordCount=Number(_recordCount);
this.totalPage=this.calculateTotalPage(this.recordCount);
if(this.totalPage<2){
this.htmlDiv.css("display","none");
}else{
this.htmlDiv.css("display","inline");
}
this.setButtonStyle();
this.htmlRecordCount.html(this.recordCount);
this.htmlTotalPage.html(this.totalPage);
this.htmlInputPage.val(this.currentPage);
this.htmlInputPage.attr("maxlength",this.maxLength());
},
constructor:Pager
}




使用方法如下:
1、对于无需刷新的页面应用:
第一步:导入jquery.js
<script src="jquery.js"></script>
第二步:导入分页组件
<script src="pager.js"></script>
第三步:在页面上声明一个全局的js变量,
var   pager;
第四步:在页面的初始化方法中实例化这个分页变量
$(function(){
pager=new Pager(0,//总的记录数
                   pageSize,//每页显示的记录数
                   ‘pager’,//这是一个页面中的div元素的id,它就是分页组件的容器
function(currentPage){//传给分页组件的回调函数                       mySheet.DoSearch("queryBudgetData.jsp",searchParam+"&currentPage="+currentPage);}


);
})
第五步:在页面中设定容纳分页组件元素的div
<div id="pager"></div>
第六步:当在页面中执行查询时,将查询到的总的记录数传给分页组件
pager.reDraw(recordCount);

2、对于需要刷新的页面的使用方法和上面的前五步一样,不需要第六步,在第四步实例化这个分页组件的时候,最后要把当前页给传进去就行了,并且第一个参数应该是总的记录数
pager=new Pager(recordCount,pageSize,"pager",function(currentPage){mySheet.DoSearch("queryBudgetData.jsp",searchParam+"&currentPage="+currentPage);
},currentPage);

因为我的这个应用是基于无刷新的,所以需要刷新的情况我没有测试,大概应该没问题,需要用的同学可以试试,不行的话就修改源代码。
这是小弟第一次尝试用OO的方式来写js,很是兴奋,不足之处,欢迎大家指教。



[解决办法]
很不错的JQEURY PAGINATION哦。
[解决办法]
最好提供个下载链接,同时提供一个使用的DEMO。
当然上面的说明也要附为readme.txt里最好了。
[解决办法]
楼主好人,谢谢分享!
[解决办法]
谢谢分享
[解决办法]
谢谢分享

[解决办法]
很不错的分页组件,我们开发的项目中已经应用了。
[解决办法]
支持!
[解决办法]
顶楼主
[解决办法]
面向对象的纯js分页组件,该如何处理
[解决办法]
复杂了一点点,其实可以做很多简化跟优化的.自己用的跟你的完全不同,没有jquery,同样是ajax,分页只用到两个函数,一个是fy,一个是send_to_search,send_to_search是点击后查询分页数据


function fy(L,l,All,s_r,page,tj,id)//L分页长度,l是每页数量,All是总量,s_r是输入的页数,后面的可有可无,比较粗糙还没优化好
        {
s_r=Math.floor(s_r);


            var min = 0;
            var max = 0;
            var D = 0;

            var pagecount = (All % l == 0) ? (All / l) : (Math.floor(All / l) + 1);//页数

            var L_C =(L % 2 == 0) ? (L / 2) : (Math.floor(L / 2) + 1);//页码居中

            if (s_r <= 0 
[解决办法]
 s_r > pagecount)//第一种情况:输入数不在有效的页数内,所以把它定位到第一页
            {
                min = 1;
                D = 1;
                max = (pagecount > L) ? L : pagecount;

            }
            else
            {
                if (pagecount <= L)//第二种情况:当总页数小于页码长度时
                {
                    min = 1;
                    D = s_r;
                    max = pagecount;
                }
                else
                {
                    if (s_r <= L_C)//第三种情况:
                    {


                        min = 1;
                        D = s_r;
                        max = L;
                    }
                    else
                    {
                        if ((pagecount - s_r)<= L_C)//第四种情况:
                        {
                            min = pagecount - L + 1;
                            D = s_r;
                            max = pagecount;
                        }
                        else
                        {
                            min = s_r - L_C + 1;
                            D = s_r;
                            max =(L % 2 == 0) ? (s_r + L_C) : (s_r + L_C - 1);
//alert(s_r +"___"+ L_C +"____"+ 1)


//alert(min+"____"+max+"____"+D+"____"+s_r+"____"+L_C+"____"+L+"____"+pagecount)
                        }
                    }
                }
            }

            var a = "";
            if (tj != null)
            {
                for(var c in tj)
                {    
    
                    a += "&" + tj[c];
if(c==tj.length-1)
    {
break;

                }
            }

            var fy = "";

            for (var kg = min; kg <= max; kg++)
            {
//alert(D);
                fy += (kg == D) ? "<span class=box>" + kg + "</span>&nbsp" : "&nbsp;<span class=box2 onclick="send_to_search("+kg+","+id+")" >" + kg + "</span>&nbsp;";
            }
            var FY = "<span class=box2 onclick="send_to_search("+1+","+id+")" >首页</span>&nbsp;&nbsp;<span class=box2 onclick="send_to_search("+(D == 1 ? 1 : (D - 1))+","+id+")" >上一页</span>&nbsp;" + fy + "&nbsp;<span class=box2 onclick="send_to_search("+((D < max) ? (D == 1 ? 2 : (D + 1)) : ((max == 0 ? max + 1 : max)))+","+id+")" >下一页</span>&nbsp;&nbsp;<span class=box2 onclick="send_to_search("+pagecount+","+id+")" >尾页</span>&nbsp;&nbsp;<span>定位到第<input type="text" class="box2" value=""+kg+"" style="width:32px; height:20px;margin: 0px 0px 0px 0px;padding:0px 0px 0px 0px;text-align:center;" />页</span><span class=box2 onclick="send_to_search("+pagecount+","+id+")" >点击定位</span>";


//maxp=pagecount;
            return FY;
        }


[解决办法]
楼主好样的,赞 面向对象的纯js分页组件,该如何处理
[解决办法]
支持支持

热点排行