﻿/*
******生成js分页脚******
****没剑(2008-03-05)****
修改日期：2008-3-12
添加两个参数：displaynum，displaylastNum可以自由定制显示的页码数量

参数：  pagesize:10  //每页显示的页码数
        ,count:0                //数据条数
        ,css:"mj_zhPagination"      //分页脚css样式类
        ,current:1              //当前页码
		,displaynum:7			//中间显示页码数
		,displaylastNum:5		//最后显示的页码数
        ,previous:"上一页"      //上一页显示样式
        ,next:"下一页"          //下一页显示样式
        ,paging:null            //分页事件触发时callback函数
        
使用：
	$("div").zhPagination({
	    pagesize:10,
	    count:500,
	    css:"mj_zhPagination",
	    previous:"<",
	    next:">",
	    paging:function(page){
			    alert("当前第"+page+"页");
		    }
	});
	以上代码为所有div加上分页脚代码
*/
jQuery.zhPagination =
{
    //生成分页脚
    create: function(_this, s) {
        var pageCount = 0;
        //计算总页码
        pageCount = (s.count / s.pagesize <= 0) ? 1 : (parseInt(s.count / s.pagesize) + ((s.count % s.pagesize > 0) ? 1 : 0));
        s.current = (s.current > pageCount) ? pageCount : s.current
        //循环生成页码
        var strPage = "<a href='1' >首页</a>";
        //创建上一页
        if (s.current <= 1) {
            strPage += "<span class=\"disabled\">" + s.previous + "</span>";
        } else {
            strPage += "<a href=\"" + (parseInt(s.current) - 1) + "\">" + s.previous + "</a>";
        }
        //开始的页码
        var startP = 1;
        startP = 1;
        var anyMore; //页码左右显示最大页码数
        anyMore = parseInt(s.displaynum / 2)
        //结束的页码
        var endP = (parseInt(s.current) + anyMore) > pageCount ? pageCount : parseInt(s.current) + anyMore;

        //可显示的码码数(剩N个用来显示最后N页的页码)
        var pCount = s.pagesize - s.displaylastNum;
        if (s.current > s.displaynum) {//页码数太多时，则隐藏多余的页码
            startP = parseInt(s.current) - anyMore;
            for (i = 1; i <= s.displaylastNum; i++) {
                strPage += "<a href=\"" + i + "\">" + i + "</a>";
            }
            strPage += "...";
        }
        if (parseInt(s.current) + parseInt(s.displaynum) <= pageCount) {//页码数太多时，则隐藏前面多余的页码
            endP = parseInt(s.current) + anyMore;
        } else {
            endP = pageCount;
        }
        for (i = startP; i <= endP; i++) {
            if (parseInt(s.current) == i) {
                strPage += "<span class=\"current\">" + i + "</span>";
            } else {
                strPage += "<a href=\"" + i + "\">" + i + "</a>";
            }
        }
        if (parseInt(s.current) + parseInt(s.displaynum) <= pageCount) {//页码数太多时，则隐藏后面多余的页码
            strPage += "...";
            for (i = pageCount - parseInt(s.displaylastNum) + 1; i <= pageCount; i++) {
                strPage += "<a href=\"" + i + "\">" + i + "</a>";
            }
        }
        //创建下一页
        if (parseInt(s.current) >= pageCount) {
            strPage += "<span class=\"disabled\">" + s.next + "</span>";
        } else {
            strPage += "<a href=\"" + (parseInt(s.current) + 1) + "\">" + s.next + "</a>";
        }
        //尾页
        strPage += "<a href=\"" + pageCount + "\">尾页</a>";
        //跳转页
        strPage += "<select  id='selectPages'  onchange='javascript:$.zhPagination.paging(this.value," + s.paging + ")'>";
        for (j = 1; j <= pageCount; j++) {
            if (j == s.current) {
                strPage += "<option value='" + j + "' selected='true'>" + j + "</option>";
            }
            else {
                strPage += "<option value='" + j + "'>" + j + "</option>";
            }
        }
        strPage += "</select>";

        $(_this).empty().append(strPage).find("a").click(function() {
            //得到翻页的页码
            var ln = this.href.lastIndexOf("/");
            var href = this.href;
            var page = parseInt(href.substring(ln + 1, href.length));
            s.current = page;
            //外部取消翻页时...
            if (!$.zhPagination.paging(page, s.paging))
                return false;

            $.zhPagination.create(_this, s);
            return false;
        });
        return this;
    },
    paging: function(page, callback) {
        if (callback) {
            if (callback(page) == false)
                return false;
        }
        return true;
    }
}


jQuery.fn.zhPagination= function(opt)
{
	/*参数定义*/
    var setting = {pagesize:10  //每页显示的页码数
        ,count:0                //数据条数
        ,css:"mj_zhPagination"      //分页脚css样式类
        ,current:1              //当前页码
		,displaynum:7			//中间显示页码数
		,displaylastNum:5		//最后显示的页码数
        ,previous:"上一页"      //上一页显示样式
        ,next:"下一页"          //下一页显示样式
        ,paging:null            //分页事件触发时callback函数
	};
	opt= opt || {}
	$.extend(setting, opt);
    return this.each(function(){
        $(this).addClass(setting.css);
        $.zhPagination.create(this,setting);
    });
}
