public class PageHtml<T> {

    public Map showPageHtml(Integer currentPage, Integer pageSize, Integer count) {

        //计算总页数
        int st = 0, ed = 0, all_pager = 0;
        int page_merchant = count / pageSize;
        int page_remainder = count % pageSize;
        if (page_remainder > 0) {
            all_pager = page_merchant + 1;
        } else {
            all_pager = page_merchant;
        }
        Map<String, Object> map = new HashMap<String, Object>();

        if (all_pager < 5) {
            st = 1;
            ed = all_pager + 1;//5 + 1;
        } else {
            if (currentPage < 3) {
                st = 1;
                //最多显示5个页码
                ed = 5 + 1;
            } else {
                if (currentPage + 2 < all_pager) {
                    st = currentPage - 2;
                    ed = currentPage + 2 + 1;
                } else {
                    st = all_pager - 5 + 1;
                    ed = all_pager + 1;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<nav aria-label=\"Page navigation\">");
        sb.append("<ul class=\"pagination\">");
        sb.append("<li>");
        sb.append("<a aria-label=\"Previous\" onclick=\"prevPage()\">");
        sb.append("<span aria-hidden=\"true\">«</span>");
        sb.append("</a>");
        sb.append("</li>");
        sb.append("<li><a onclick=\"firstPage()\">首页</a></li>");
        for (int i = st; i < ed; i++) {
            if (i == currentPage) {
                sb.append("<li class=\"active\" id=\"selected\"><a onclick=\"pageIndex(" + i + ")\">" + i + "</a></li>");
            } else {
                sb.append("<li><a onclick=\"pageIndex(" + i + ")\">" + i + "</a></li>");
            }
        }
        sb.append("<li><a  onclick=\"lastPage()\">尾页</a></li>");
        sb.append("<li>");
        sb.append("<a aria-label=\"Next\" onclick=\"nextPage()\">");
        sb.append("<span aria-hidden=\"true\">»</span>");
        sb.append("</a>");
        sb.append("</li>");
        sb.append("</ul>");
        sb.append("</nav>");
        String html = sb.toString();
        map.put("conut", count);
        map.put("html", html);
        map.put("pageTotal", all_pager);
        return map;
    }
}

 

 

 

参考

https://www.cnblogs.com/Dominic-Ji/articies/12035722.html

posted on 2021-06-30 16:41  23云恋49枫  阅读(33)  评论(0编辑  收藏  举报