js分頁例子

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>JS分頁Class – 風幹的果子</title>
    <style type="text/css">
a {margin:3px; text-decoration:none;}
</style>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

    <script type="text/javascript">
    //jquery頁面加載開始
$(function()
{

    var pageDiv1=new jsPage("list","all","p1","3");
    pageMethod.call(pageDiv1);

    var pageDiv2=new jsPage("list2","all","p2","5");
    pageMethod.call(pageDiv2);
})//$
//jquery 頁面加載結束
//方法: pageMethod
function pageMethod()
{
    var obj=this;
    obj.resetTotal(); 
    obj.reloadpage("1",obj.list_class);
    obj.page(); //生成頁碼
    ready2go.call(obj);
}
//方法:ready2go
function ready2go()
{
    var obj=this;
    $("#"+obj.page_obj_id+" a").live("click",function()
    { //點擊頁碼的時候跳到相應頁   
    obj.target_p=parseInt($(this).attr("p"));
    gotopage.call(obj,obj.target_p);
    })

    
}
// 方法: showClass
function showClass(list,x,pDiv,pSize){   
    var pObj=new jsPage(list,x,pDiv,pSize);
    pageMethod.call(pObj);
}
// 方法: jsPage
function jsPage(list_id,list_class,page_obj_id,pagesize)
{
    // list_id 結果集UL的id
    // list_class 要顯示的類別
    // page_id 存放頁碼的id
// pagesize 每頁顯示多少條
// QQ群號: 29032448
    this.list_obj_id=list_id;
    this.list_Obj=$("#"+list_id);
    this.li=$("#"+list_id+" li");
    this.li.hide();
    this.page_obj_id=page_obj_id;
    this.page_obj=$("#"+page_obj_id); //存放頁碼的p
    this.list_class=list_class; // 類別      
    if(list_class=="all")
    {
        this.results=this.li.length; // 總記錄數等於所有記錄
    }
    else
    {
       this.results=$("#"+list_id+" li."+list_class).length; // 總記錄數等於指定類別的li數 
    } 
   
    this.totalpage; // 總頁數
    this.pagesize=pagesize; //每頁記錄數
    this.cpage=1; //當前頁,默認顯示第一頁
    this.count;
    this.target_p;
    this.curcount;
    this.outstr= ""; // 輸出頁碼html      
}//jsPage結束
//方法: gotopage
function gotopage(target){     
    this.cpage = target;        //把頁面計數定位到第幾頁
    this.page();
    this.reloadpage(target,this.list_class);   
}
//給對象jsPage 增加函數 reloadpage
jsPage.prototype.reloadpage=function(p,resultType)
{
    this.li.hide();
    for(var i=this.pagesize*p-this.pagesize;i<this.pagesize*p;i++)
    {
       if(resultType=="all")
       {
        this.li.eq(i).show();
       }else
       {
        $("#"+this.list_obj_id+" li."+resultType).eq(i).show();
       }
    }
}
//給對象jsPage 增加函數 resetTotal
jsPage.prototype.resetTotal=function()
{
    if(this.results==0){
       this.totalpage=0;
       this.cpage=0;
    }else if(this.results<=this.pagesize)
    {
       this.totalpage=1;
    }
    else if(parseInt(this.results/this.pagesize)==1)
    {
       this.totalpage=2;
    }
    else if(parseInt(this.results/this.pagesize)>1 && this.results%this.pagesize==0){
       this.totalpage=this.results/this.pagesize;
    }
    else
    {
       this.totalpage=parseInt(this.results/this.pagesize)+1;
    }
}//resetTotal()
//jsPage 對象增加函數 page
jsPage.prototype.page=function()
{
    if(this.totalpage<=10)
    {        //總頁數小於十頁   頁碼以十頁為單位
        for (this.count=1;this.count<=this.totalpage;this.count++)
        {   
            if(this.count!=this.cpage)
            {
                this.outstr = this.outstr + "<a href='javascript:void(0)' p='"+this.count+"' >"+this.count+"</a>";
            }else
            {
                this.outstr = this.outstr + "<span class='current' >"+this.count+"</span>";
            }
        }
    }
    if(this.totalpage>10)
    {        //總頁數大於十頁
        if(parseInt((this.cpage-1)/10) == 0)
        {           
            for (this.count=1;this.count<=10;this.count++)
            {   
                if(this.count!=this.cpage)
                {
                    this.outstr = this.outstr + "<a href='javascript:void(0)' p='"+this.count+"' >"+this.count+"</a>";
                }
                else
                {
                    this.outstr = this.outstr + "<span class='current'>"+this.count+"</span>";
                }
            }
            this.outstr = this.outstr + "<a href='javascript:void(0)' p='"+this.count+"' >&raquo;</a>";
        }
        else if(parseInt((this.cpage-1)/10) == parseInt(this.totalpage/10))
        {   
            this.outstr = this.outstr + "<a href='javascript:void(0)' p='"+(parseInt((this.cpage-1)/10)*10)+"' >&laquo;<\/a>";
            for (this.count=parseInt(this.totalpage/10)*10+1;this.count<=this.totalpage;this.count++) 
             {
                if(this.count!=this.cpage)
                {
                    this.outstr = this.outstr + "<a href='javascript:void(0)' p='"+this.count+"' >"+this.count+"</a>";
                }
                else
                {
                    this.outstr = this.outstr + "<span class='current'>"+this.count+"</span>";
                }
            }
        }
        else
        {  
            var lastP;
            this.outstr = this.outstr + "<a href='javascript:void(0)' p='"+(parseInt((this.cpage-1)/10)*10)+"' >&laquo;<\/a>";
            for (this.count=parseInt((this.cpage-1)/10)*10+1;this.count<=parseInt((this.cpage-1)/10)*10+10;this.count++)
            {       
                if(this.count!=this.cpage)
                {
                    this.outstr = this.outstr + "<a href='javascript:void(0)' p='"+this.count+"' >"+this.count+"</a>";
                }
                else
                {
                    this.outstr = this.outstr + "<span class='current'>"+this.count+"</span>";
                }
                if(this.count==this.totalpage)
                {
                    lastP="";
                }
                else
                {
                    lastP="<a href='javascript:void(0)' p='"+(this.count+1)+"' >&raquo;</a>";
                }
            }
            this.outstr = this.outstr + lastP;
        }
    }   
    document.getElementById(this.page_obj_id).innerHTML = "<p><span id='info'>共"+this.totalpage+"頁 第"+this.cpage+"頁<\/span>" + this.outstr + "<\/p>";
    this.outstr = "";
}
    </script>

</head>
<body>
    <a onclick="showClass('list','mm','p1','3')" href="#">美女</a> <a onclick="showClass('list','gg','p1','3')"
        href="#">帥哥</a> <a onclick="showClass('list','all','p1','3')" href="#">全部</a><br />
    <ul id="list">
        <li class="mm">美女A</li>
        <li class="gg">帥哥A</li>
        <li class="mm">美女B</li>
        <li class="gg">帥哥B</li>
        <li class="mm">美女C</li>
        <li class="gg">帥哥C</li>
        <li class="mm">美女D</li>
        <li class="gg">帥哥D</li>
        <li class="mm">美女E</li>
        <li class="gg">帥哥E</li>
        <li class="mm">美女F</li>
        <li class="gg">帥哥F</li>
        <li class="mm">美女G</li>
        <li class="gg">帥哥G</li>
        <li class="mm">美女H</li>
        <li class="gg">帥哥H</li>
        <li class="mm">美女A</li>
        <li class="gg">帥哥A</li>
        <li class="mm">美女B</li>
        <li class="gg">帥哥B</li>
        <li class="mm">美女C</li>
        <li class="gg">帥哥C</li>
        <li class="mm">美女D</li>
        <li class="gg">帥哥D</li>
        <li class="mm">美女E</li>
        <li class="gg">帥哥E</li>
        <li class="mm">美女F</li>
        <li class="gg">帥哥F</li>
        <li class="mm">美女G</li>
        <li class="gg">帥哥G</li>
        <li class="mm">美女H</li>
        <li class="gg">帥哥H</li>
        <li class="mm">美女A</li>
        <li class="gg">帥哥A</li>
        <li class="mm">美女B</li>
        <li class="gg">帥哥B</li>
        <li class="mm">美女C</li>
        <li class="gg">帥哥C</li>
        <li class="mm">美女D</li>
        <li class="gg">帥哥D</li>
        <li class="mm">美女E</li>
        <li class="gg">帥哥E</li>
        <li class="mm">美女F</li>
        <li class="gg">帥哥F</li>
        <li class="mm">美女G</li>
        <li class="gg">帥哥G</li>
        <li class="mm">美女H</li>
        <li class="gg">帥哥H</li>
        <li class="mm">美女A</li>
        <li class="gg">帥哥A</li>
        <li class="mm">美女B</li>
        <li class="gg">帥哥B</li>
        <li class="mm">美女C</li>
        <li class="gg">帥哥C</li>
        <li class="mm">美女D</li>
        <li class="gg">帥哥D</li>
        <li class="mm">美女E</li>
        <li class="gg">帥哥E</li>
        <li class="mm">美女F</li>
        <li class="gg">帥哥F</li>
        <li class="gg">帥哥G</li>
        <li class="gg">帥哥H</li>
        <li class="gg">帥哥G</li>
    </ul>
    <br />
    <p id="p1">
    </p>
    <br />
    <br />
    <a onclick="showClass('list2','music','p2','5')" href="#">音樂</a> <a onclick="showClass('list2','movie','p2','5')"
        href="#">電影</a> <a onclick="showClass('list2','all','p2','5')" href="#">全部</a><br />
    <ul id="list2">
        <li class="music">音樂A</li>
        <li class="movie">電影A</li>
        <li class="music">音樂B</li>
        <li class="movie">電影B</li>
        <li class="music">音樂C</li>
        <li class="movie">電影C</li>
        <li class="music">音樂D</li>
        <li class="movie">電影D</li>
        <li class="music">音樂E</li>
        <li class="movie">電影E</li>
        <li class="music">音樂F</li>
        <li class="movie">電影F</li>
    </ul>
    <br />
    <p id="p2">
    </p>
</body>
</html>

 

作者 鄭文亮

發佈留言