日期:2014-05-17  浏览次数:20635 次

GOOGLE分页算法
Java code

        /**
     * GOOGLE 分页
     * @param curpage 当前页
     * @param showNum 页面显示的页数
     * @param pages      实际页数
     * @return
     */
    protected Integer[] pagesSplit(Integer curpage, Integer showNum, Integer pages ) {
        
        Integer startIndex = curpage;
        
        Integer endIndex = showNum;
        
        endIndex = showNum + startIndex - 1;
    
        if(endIndex > pages) {
        
            endIndex = pages;
        
        }
    
        if(endIndex - startIndex + 1 == showNum) {
        
            endIndex = showNum;
    
        }
        
        startIndex = endIndex - showNum + 1;
        
        if(endIndex - startIndex + 1 < showNum) {
        
            startIndex = endIndex - startIndex + 1;
    
        }
        
        if(startIndex <= 0) {
        
            startIndex = 1;
    
        }
        
        if(endIndex <= 0) {
        
            endIndex = 0;
    
        }
    
        return new Integer[]{startIndex, endIndex};
    
    }




此方法返回就是页面显示的 起始页 和 结束页。

如页面输出的链接从第5页开始, showNum为10,当实际页数大于10页的时候页面就可以输出:

5 6 7 8 9 10 11 12 13 14

如页面输出的链接从第5页开始, showNum为10,当实际页数不够10页,实际页数为8页的时候页面就可以输出:

1 2 3 4 5 6 7 8


页面输出分页链接:

Java code

   
  <c:forEach  var="current" begin="${startIndex}" end="${endIndex}">
     <a href="">${current}</a>
  </c:forEach>





------解决方案--------------------
强悍 ! ! !
------解决方案--------------------
看头像, 拿分, 闪人!