Paging

programing/JAVA 2009.06.02 18:01

public class Paging {

    private String pagingCode;

    public Paging(int totalRecord, int nowPage, int numPerPage, int numPerBlock, String url, String param) {
        int totalPage = (int) Math.ceil((double) totalRecord / numPerPage); //double로 소수점까지표시후  Math.ceil로 올림
        int totalBlock = (int) Math.ceil((double) totalPage / numPerBlock);
        int nowBlock = (int) Math.ceil((double) nowPage / numPerBlock);

        StringBuffer prev = new StringBuffer();
        StringBuffer next = new StringBuffer();
        StringBuffer paging = new StringBuffer();

        if (nowBlock > 1) {
            prev.append("<a href='");
            prev.append(url);
            prev.append("?nowPage=");
            prev.append((nowBlock - 2) * numPerBlock + 1);
            prev.append("' style='color:black;text-decoration:none;'>");
            prev.append("<span style='font-family:Dotum;font-weight:bold;");
            prev.append("font-size:14px;text-align:center;cursor:pointer'>");
            prev.append("&lt;이전</span></a>&nbsp;&nbsp;");
        } else {
            prev.append("<span> </span>&nbsp;&nbsp;");
        }

        for (int i = 0; i < numPerBlock; i++) {
            int printPage = ((nowBlock - 1) * numPerBlock) + i + 1;
            if (printPage == nowPage) {     //자기페이지는 클릭 X
                paging.append("<span style='font-family:Dotum;font-weight:bold;font-size:14px;");
                paging.append("text-align:center;color:orange'>");
                paging.append(printPage);
                paging.append("</span>&nbsp;&nbsp;");
            } else {
                paging.append("<a href='");
                paging.append(url);
                paging.append("?nowPage=");
                paging.append(printPage);
                if (param != null) {
                    paging.append(param);
                }
                paging.append("' style='color:black;text-decoration:none;'>");
                paging.append("<span style='font-family:Dotum;font-weight:bold;");
                paging.append("font-size:14px;text-align:center;cursor:pointer'>");
                paging.append(printPage);
                paging.append("</span></a>&nbsp;&nbsp;");
            }
            if (printPage >= totalPage) {       //마지막 블럭은 페이지 있는데까지만 나오도록
                break;
            }
        }

        if (totalBlock > nowBlock) {
            next.append("<a href='");
            next.append(url);
            next.append("?nowPage=");
            next.append(nowBlock * numPerBlock + 1);
            next.append("' style='color:gray;text-decoration:none;'>");
            next.append("<span style='font-family:Dotum;font-weight:bold;");
            next.append("font-size:14px;text-align:center;cursor:pointer'>");
            next.append("다음&gt;</span></a>");
        } else {
            next.append("<span> </span>");
        }

        pagingCode = prev.toString() + paging.toString() + next.toString();
    }

    public String getPagingCode() {
        return pagingCode;
    }

    public void setPagingCode(String pagingCode) {
        this.pagingCode = pagingCode;
    }
}

신고

'programing > JAVA' 카테고리의 다른 글

계층형 게시판  (0) 2009.06.03
File Up & Down  (0) 2009.06.02
Paging  (0) 2009.06.02
FileUtil  (0) 2009.06.02
JSP ibatis+Struts2  (0) 2009.06.02
ConnectionPool  (0) 2009.06.02
Posted by 대절님

댓글을 달아 주세요



티스토리 툴바