pc端表格分页的简单实现

(function ($) {
    var obj = obj || {};
    var currentPage = 1;//默认第一页
    var pageSize = 10;//每页显示条数
    var pageNum='';//总页数
    obj.search=function(){
        var _this=this;
        $("#search").click(function(){
            currentPage=1;
            _this.selectH5Order(true);
            obj.fillHtml('pageHtml',{"currPage":currentPage,"pageNum":pageNum,"state":obj.state()});
        })

    }  


    obj.init=function(pageHtml,args){
        return (function(){
            obj.bindEvent();
            obj.search();
        })();
    };

       //var currentPage=1;var totalPage;

    obj.fillHtml=function(pageHtml,args){
            return (function(){
                pageHtml="";
                if(currentPage > 1){
                    pageHtml += "<a href='javascript:void(0);' class='geraltTb_pager' data-go='' >上一页</a>";
                }else{
                    pageHtml += "";
                }
                // 判断出现...情况
                if(currentPage!=1 && currentPage>=4 && args.pageNum!=4) {
                    pageHtml += "<a href='javascript:void(0);' class='geraltTb_pager' data-go='' >首页</a>";
                }
                if(currentPage-2>2 && currentPage<=args.pageNum && args.pageNum>5) {
                    pageHtml += "<a href='javascript:void(0);' class='geraltTb_' data-go='' >...</a>";
                }

                var start = currentPage-2;//通过当前页面跟总页数关系来判断
                var end = currentPage+2;
                if((start>1 && currentPage<4) || currentPage==1) {
                    end++;
                }
                if(currentPage>args.pageNum-4 && currentPage>=args.pageNum) {
                    start--;
                }       
                for(; start<=end; start++) {
                    if(start<=args.pageNum && start>=1) {
                        // if(start != currentPage) {
                        //  pageHtml += "<li class='ali'><a href='javascript:void(0);' class='geraltTb_pager' data-go='' >"+start+"</a>";
                        // }else{
                            pageHtml += "<a href='javascript:void(0);' class='geraltTb_pager' data-go='' >"+start+"</a>";
                        // }
                    }
                }

                if(currentPage+2<args.pageNum-1 && currentPage>=1 && args.pageNum>5) {
                    pageHtml += "<a href='javascript:void(0);' class='geraltTb_' data-go='' >...</a>";
                }

                if(currentPage!=args.pageNum && currentPage<args.pageNum-2 && args.pageNum!=4) {
                    pageHtml += "<a href='javascript:void(0);' class='geraltTb_pager' data-go='' >末页</a>";
                }

                if(currentPage < args.pageNum){
                    pageHtml += "<a href='javascript:void(0);' class='geraltTb_pager' data-go='' >下一页</a>";
                }else{
                    pageHtml += "";
                }
                $(".pagination").html(pageHtml);
            })();
    };      
    obj.bindEvent=function(){
            return (function(){
                $(".page").on("click","a.geraltTb_pager",function(event){
                    $(this).addClass("cur").siblings().removeClass("cur");

                    if($(this).text()=="上一页"){
                        currentPage=(currentPage>1)?currentPage-1:1;
                    }
                    else if($(this).text()=="下一页"){
                        currentPage=(currentPage<pageNum)?currentPage+1:pageNum;
                    }else if($(this).text()=="首页"){
                        currentPage=1;
                    }else if($(this).text()=="末页"){
                        currentPage=pageNum;
                    }else{
                        currentPage = parseInt($(this).text());
                    }
                    // obj.fillHtml(pageHtml,{"starTime":"","endTime":"","currPage":currentPage,"pageNum":args.pageNum,"turndown":args.turndown,"state":obj.state()});
                    // if(typeof(args.backFn)=="function"){
                    //     args.backFn(args.currentPage);
                    // }
                    obj.selectH5Order();
                });

            })();
    }

    obj.selectH5Order=function(first){
            var _this=this; 
            var trHtml='';//列表  

           //获取数据
            $.ajax({
                url:'xxxx',
                type:'post',
                dataType:'json',
                cache:true,
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                data:XXX,
                success: function (data) {
                    if(data.code="0000"){
                       pageNum=Math.ceil(data.listSize/pageSize);//通过接口反的数据,算出总页数
                        var ORDERSTATUS;
                        // if(first&&pageNum>0){_this.getData()}
                        // _this.fillHtml();
                        // if(first){trHtml=''};
                        trHtml+='<tr>XX<td>XX</td></tr>'

                            }
                            $("table").eq(0).html(trHtml);
                        }else{
                            pageNum=0;

                            $("table").html(trHtml);

                        }
                        obj.fillHtml('pageHtml',{"starTime":"","endTime":"","currPage":currentPage,"pageNum":pageNum,"state":obj.state()});


                    }else{
                            alert(data.message)
                        }


                    }
                },
                error:function(){


                }

            })
    }

    return {
        selectH5Order:obj.selectH5Order(true),
        bindEvent:obj.init()

    }
})(jQuery);

package com; public class Pager { private int totalRows = 0; // 记录总数 private int totalPages = 0; // 总页数 private int pageSize = 10; // 每页显示数据条数,默认为10条记录 private int currentPage = 1; // 当前页数 private boolean hasPrevious = false; // 是否有上一页 private boolean hasNext = false; // 是否有下一页 public int getSearchFrom() { return (currentPage - 1) * pageSize; } public Pager() { } public void init(int totalRows) { this.totalRows = totalRows; this.totalPages = ((totalRows + pageSize) - 1) / pageSize; refresh(); // 刷新当前页面信息 } /** * * @return Returns the currentPage. * */ public int getCurrentPage() { return currentPage; } /** * * @param currentPage * current page * */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; refresh(); } /** * * @return Returns the pageSize. * */ public int getPageSize() { return pageSize; } /** * * @param pageSize * The pageSize to set. * */ public void setPageSize(int pageSize) { this.pageSize = pageSize; refresh(); } /** * * @return Returns the totalPages. * */ public int getTotalPages() { return totalPages; } /** * * @param totalPages * The totalPages to set. * */ public void setTotalPages(int totalPages) { this.totalPages = totalPages; refresh(); } /** * * @return Returns the totalRows. * */ public int getTotalRows() { return totalRows; } /** * * @param totalRows * The totalRows to set. * */ public void setTotalRows(int totalRows) { this.totalRows = totalRows; refresh(); } // 跳到第一页 public void first() { currentPage = 1; this.setHasPrevious(false); refresh(); } // 取得上一页(重新设定当前页面即可) public void previous() { if (currentPage > 1) { currentPage--; } refresh(); } // 取得下一页 public void next() { //System.out.println("next: totalPages: "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值