zb51500618 2016-04-19 01:58 采纳率: 83.3%
浏览 1407
已采纳

运用工具类分页 页面怎么写?

我对分页的页面不知道怎么下手
工具类 在页面 如何使用不清楚,求高人指点

 public class Pagination implements Serializable {  

        private static final long serialVersionUID = 1L;  
        private int start; // start表示当前页开始的记录数,start=每页行数*(当前页数-1)  
        private int end; // 当前页结束的记录行数  
        private int totalCount; // 总行数  
        private int rowsPerPage =CommodtiyConstants.ROWS_PER_PAGE  ; // 每页行数,默认15  
        private int currentPage; // 当前页数  
        private int pageListSize = CommodtiyConstants.PAGE_LIST_SIZE;// 页码列表大小,默认9  
        private List<Integer> pageNumList = new ArrayList<Integer>();  
  public Pagination() {  
            start = 0;  
            end = 0;  
            currentPage = 1;  
            this.totalCount = 0;  
        }  

        public Pagination(int totalCount, int numPerPage) {  
            start = 0;  
            end = 0;  
            this.totalCount = totalCount;  
            currentPage = 1;  
            if (numPerPage > 0) {  
                rowsPerPage = numPerPage;  
            }  
        }  

        /** 
         * 执行翻页动作 
         *  
         * @param currentPage 
         *            要翻到的目标页码 
         * @return 返回翻页对象 
         */  
        public Pagination doPagination(int currentPage) {  
            gotoPage(currentPage);  
            return this;  
        }  

        // 设置起始数  
        public int getStart() {  
            start = rowsPerPage * (currentPage - 1);  
            return start;  
        }  

        // 得到起始数  
        public void setStart(int start) {  
            if (start < 0) {  
                this.start = 0;  
            } else if (start >= this.totalCount) {  
                this.start = this.totalCount - 1;  
            } else {  
                this.start = start;  
            }  
        }  

        // 设置当前页的最后一行的在总记录中的顺序(从0开始)  
        public void setEnd(int end) {  
            this.end = end;  
        }  

        // 得到当前页的最后一行的在总记录中的顺序(从0开始)  
        public int getEnd() {  
            if (rowsPerPage * currentPage > this.totalCount) {  
                end = this.totalCount - 1;  
            } else {  
                end = rowsPerPage * currentPage - 1;  
            }  
            return end;  
        }  

        // 以下4个方法供控制器(struts)调用  

        // 判断能否到第一页;只要能到上一页,肯定就有第一页  
        public boolean firstEnable() {  
            return previousEnable();  
        }  

        // 判断能否到上一页  
        public boolean previousEnable() {  
            return currentPage > 1;// 只要不是第一页,就能到上一页  
        }  

        // 判断能否到下一页  
        public boolean nextEnable() {  
            return currentPage * rowsPerPage < this.totalCount;  
        }  

        // 判断能否到最后一页;只要有下一页,就肯定有最后一页.  
        public boolean lastEnable() {  
            return nextEnable();  
        }  

        // 跳到第一页  
        public void firstPage() {  
            currentPage = 1;  
        }  

        // 跳到上一页  
        public void previousPage(int cPage) {  
            currentPage = (cPage - 1) > 0 ? (cPage - 1) : 1;  
        }  

        // 跳到下一页  
        public void nextPage(int cPage) {  
            currentPage = cPage + 1;  
            if (currentPage * rowsPerPage > this.totalCount) {  
                lastPage();  
            }
        }  

        // 跳到最后一页  
        public void lastPage() {  
            if (this.totalCount % rowsPerPage == 0) {  
                currentPage = this.totalCount / rowsPerPage;  
            } else {  
                currentPage = this.totalCount / rowsPerPage + 1;  
            }  
        }  

        // 跳到指定的某一页  
        public void gotoPage(int pageNumber) {  
            if (pageNumber <= 1) {  
                currentPage = 1;  
            } else if (getTotalCount() < this.getRowsPerPage()) {  
                currentPage = 1;  
            } else if (pageNumber * rowsPerPage >= this.totalCount) {  
                lastPage();  
            } else {  
                currentPage = pageNumber;  
            }  
        }  

        // 设置总行数  
        public void setTotalCount(int totalCount) {  
            this.totalCount = totalCount;  
        }  

        // 得到总行数  
        public int getTotalCount() {  
            return totalCount;  
        }  

        // 设置每页行数  
        public void setRowsPerPage(int rowsPerPage) {  
            this.rowsPerPage = rowsPerPage;  
        }  

        // 得到每页行数  
        public int getRowsPerPage() {  
            return rowsPerPage;  
        }  

        // 得到总页数  
        public int getPages() {  
            if (this.totalCount % rowsPerPage == 0)  
                return this.totalCount / rowsPerPage;  
            else  
                return this.totalCount / rowsPerPage + 1;  
        }  

        // 得到当前页数  
        public int getCurrentPage() {  
            return currentPage;  
        }  

        // 设置当前页数  
        public void setCurrentPage(int currentPage) {  
            this.currentPage = currentPage;  
        }  

        public int getPageListSize() {  
            return pageListSize;  
        }  

        // 设置页码列表大小  
        public void setPageListSize(int pageListSize) {  
            this.pageListSize = pageListSize;  
        }  

        // 得到页面列表  
        public List<Integer> getPageNumList() {  
            this.pageNumList.removeAll(this.pageNumList);// 设置之前先清空  
            int totalPage = getPages();  
            if (totalPage > this.pageListSize) {  
                int halfSize = this.pageListSize / 2;  
                int first = 1;  
                int end = 1;  
                if (this.currentPage - halfSize < 1) { // 当前页靠近最小数1  
                    first = 1;  
                    end = this.pageListSize;  
                } else if (totalPage - this.currentPage < halfSize) { // 当前页靠近最大数  
                    first = totalPage - this.pageListSize + 1;  
                    end = totalPage;  
                } else {  
                    first = this.currentPage - halfSize;  
                    end = this.currentPage + halfSize;  
                }  
                for (int i = first; i <= end; i++) {  
                    this.pageNumList.add(i);  
                }  
            } else {  
                for (int i = 0; i < totalPage; i++) {  
                    this.pageNumList.add(i + 1);  
                }  
            }  

            return pageNumList;  
        }  
    }  

  • 写回答

2条回答 默认 最新

  • 关注

    找个分页控件呗 真要自己写的话
    肯定要考虑

    取数据 (函数) 上 下 首 尾页(四个按钮)
    .cs
    BindToForm()
    {
    int 行数=getTotalCount();
    int 当前页=getCurrentPage();
    int startIndex =当前页*行数;
    int endindex=startIndex+行数;
    string sql = string.Format("SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY name ASC) AS ROW, T.* FROM test T WHERE {0} )"
    + "TT where TT.Row between {1} and {2}", "name='张三'", startIndex + 1, endIndex);
    }
    上 下 首 尾页 听你的描述好像前面都有了我就不说了
    ,就是点击的时候绑一下数据
    (// 跳到...页 那几个函数)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度