pages.js 8.72 KB
(function($) {
    $.widget('JA.hdpPages', {
            options:{
                tableId:null,
                url:null,
                type:'GET',
                dataType:"text",
                total:0,
                btnLength:9,
                count:0,
                async:true,
                param:{},
                initialize:true,
                success:function(data,op){
                    $("#"+op.tableId).html(data);
                    var count = $("#"+op.tableId + " #listCount").val();
                    op.count = count;
                },
                error: function (data) {
                },
                pageable:{
                    page:1,
                    size:15
                }
            },

            createPageHtml:function(){
                var first_style = "";
                var op = this.options;
                if(op.pageable.page == 1){
                    first_style = 'style="color: #C6C6C6"';
                }
                var context = '';
                context += '<div style="float:left; margin: 27px 0;">'
                            +'<p style="margin:0">总共'+op.count+'条记录,每页'+op.pageable.size+'条,总共'+op.total+'页</p>'
                            +'</div><ul style="float: right" class="pagination">';
                context += 
                            '<li><a class="a_first" '+first_style+' href="javascript:void(0);">首页</a></li>'
                            +'<li><a class="a_last" '+first_style+' href="javascript:void(0);">上一页</a></li>';
                if(!op.total || op.total == 0){
                    return;
                }
                if(!op.count || op.count == 0){
                    return;
                }
                if(op.total < op.btnLength+1){
                    for(var i = 1; i <= op.total;i++){
                        //var isClickClass = "";
                        //if(i == op.pageable.page){
                        //    isClickClass = "active";
                        //}
                        //context += '<li><a class="a_page '+isClickClass+'" page="'+i+'" href="javascript:void(0);">'+i+'</a></li>';
                        context += this.setAHtml(i,op);
                    }
                }else{
                    if(op.pageable.page < 1 + (op.btnLength+1)/2){
                        for(var i = 1;i <= op.btnLength - 2; i++){
                            //var isClickClass = "";
                            //if(i == op.pageable.page){
                            //    isClickClass = "active";
                            //}
                            //context += '<li><a class="a_page '+isClickClass+'" page="'+i+'" href="javascript:void(0);">'+i+'</a></li>';
                            context += this.setAHtml(i,op);
                        }
                        context += '<li><span>...</span></li>';
                        context += '<li><a class="a_page" page='+op.total+' href="javascript:void(0);">'+op.total+'</a></li>';
                    }else if(op.pageable.page > op.total - (op.btnLength+1)/2){
                        context += '<li><a class="a_page" page="1" href="javascript:void(0);">1</a></li>';
                        context += '<li><span>...</span></li>';
                        for (var i = op.total - op.btnLength + 3; i <= op.total; i++) {
                            //var isClickClass = "";
                            //if(i == op.pageable.page){
                            //    isClickClass = "active";
                            //}
                            //context += '<li><a class="a_page '+isClickClass+'" page="'+i+'" href="javascript:void(0);">'+i+'</a></li>';
                            context += this.setAHtml(i,op);
                        }
                    }else{
                        context += '<li><a class="a_page" page="1" href="javascript:void(0);">1</a></li>';
                        context += '<li><span>...</span></li>';
                        for (var i = parseInt(op.pageable.page) - 2; i <= parseInt(op.pageable.page) + 2; i++) {
                            //var isClickClass = "";
                            //if(i == op.pageable.page){
                            //    isClickClass = "active";
                            //}
                            //context += '<li><a class="a_page '+isClickClass+'" page="'+i+'" href="javascript:void(0);">'+i+'</a></li>';
                            context += this.setAHtml(i,op);
                        }
                        context += '<li><span>...</span></li>';
                        context += '<li><a class="a_page" page='+op.total+' href="javascript:void(0);">'+op.total+'</a></li>';
                    }
                }
                var final_style = "";
                if(op.pageable.page == op.total){
                    final_style = 'style="color: #C6C6C6"';
                }
                context += '<li><a class="a_next" '+final_style+' href="javascript:void(0);">下一页</a></li>'
                            +'<li><a class="a_final" '+final_style+' href="javascript:void(0);">末页</a></li>';
                context += '</ul>';
                console.log(context);
                $(this.element).html(context);
                this.pageClick();
            },
            setAHtml:function(i,op){
                var isClickClass = "";
                if(i == op.pageable.page){
                    isClickClass = "background-color:#0e90d2;color:#fff;";
                }
                return '<li><a class="a_page" style="'+isClickClass+'" page="'+i+'" href="javascript:void(0);">'+i+'</a></li>';
            },
            pageClick:function(){
                var pa = $(this.element);
                var th = this;
                var create = th.createPages;
                pa.find(".a_first").click(function(){
                    th.options.pageable.page = 1;
                    create(th);
                });
                pa.find(".a_final").click(function(){
                    th.options.pageable.page = th.options.total;
                    create(th);
                });
                pa.find(".a_last").click(function(){
                    var page = parseInt(th.options.pageable.page) - 1;
                    if(page < 1){
                        page = 1;
                    }
                    th.options.pageable.page = page;
                    create(th);
                });
                pa.find(".a_next").click(function(){
                    var page = parseInt(th.options.pageable.page) + 1;
                    if(page > th.options.total){
                        page = th.options.total;
                    }
                    th.options.pageable.page = page;
                    create(th);
                });
                pa.find(".a_page:not(.active)").click(function(){
                    var page = $(this).attr("page");
                    th.options.pageable.page = page;
                    create(th);
                });
                //pa.find(".a_page .active").addCss("","");

            },
            createPages:function(e){
                //开始
                var ti = e;
                var op = e.options;
                e.element.html("");
                op.param.page = op.pageable.page;
                op.param.size = op.pageable.size;
                $.ajax({
                    type: op.type,
                    url: op.url,
                    data: op.param,
                    dataType: op.dataType,
                    async:op.async,
                    success: function (data) {
                            op.success(data,op);
                            var size = op.pageable.size;
                            var total = parseInt(op.count/size);
                            if(op.count%size > 0){
                                total++;
                            }
                            op.total = total;
                            ti.createPageHtml();
                    },error: function (data) {
                        op.error(data);
                    },complete: function (data) {

                        //alert(1);
                    }
                });
            },
            _create:function(){
                if(this.options.initialize){
                    this.createPages(this);
                }
            },
            getOptions:function(){
                this.options;
            },
            reflash:function(){
                this.options.pageable.page = 1;
                this.createPages(this);
            },
            reSetUrl:function(url){
                this.options.url = url;
                this.options.pageable.page = 1;
                this.createPages(this);
            },
            reSetParam:function(param){
                this.options.param = param;
            }
    });
})(jQuery);