开发博客系统的时候使用了 NVelocity 模版引擎,在列表页的时候需要使用分页,所以自己写了个生成 Html 代码的分页。
使用NVelocity生成列表页面的时候比如是第一页把参数“1”和列表页的URL等传进去,生成当前页面的分页,然后用NVelocity替换生成后的代码,需要生成的代码示例:
<div class="pager"> <div class="pagerbox"> <a href="/?cid=0&tag=&key=&page=1">首页</a> <a href="/?cid=0&tag=&key=&page=1">上一页</a> <b>1</b> <a href="/?cid=0&tag=&key=&page=2">2</a> <a href="/?cid=0&tag=&key=&page=3">3</a> <a href="/?cid=0&tag=&key=&page=4">4</a> <a href="/?cid=0&tag=&key=&page=2">下一页</a> <a href="/?cid=0&tag=&key=&page=6">尾页</a> </div> <span>当前第1页,共6页, 共有53条记录。</span> </div>
先来个效果图:
样式代码:
.pagerbox { float: left; } .pager { margin: 20px 0px; } .pager span { line-height: 28px; float: right; } .pagerbox a, .pagerbox b { background-color: #009AD9; border: 1px solid #009AD9; } .pager .pagerbox a { color: #FFF; float: left; line-height: 28px; padding: 0 10px; margin-right: 2px; } .pager .pagerbox a:hover { color: #000000; border: 1px solid #148ec0; background: #f0f0f0; } .pager .pagerbox b { float: left; font-weight: normal; padding: 0 10px; background: #fff; line-height: 28px; margin-right: 2px; }
后台代码:
///<summary> ///打印分页信息。 ///</summary> ///<param name="recordCount">记录总数。</param> ///<param name="pageIndex">分页索引。</param> ///<param name="pageSize">分页大小。</param> ///<param name="pageLink">分页地址,{0}为当前页匹配。</param> /// <param name="nextPageText">下一页显示文本。</param> /// <param name="prevPageText">上一页显示文本。</param> /// <param name="homePageText">首页显示文本。</param> /// <param name="lastPageText">尾页显示文本。</param> /// <param name="displayMsg">显示消息文本。</param> /// <returns>返回分页的 Html 代码。</returns> public static string Print(int recordCount, int pageIndex, int pageSize, string pageLink, string nextPageText, string prevPageText, string homePageText, string lastPageText, string displayMsg) { if (pageSize < 1) { pageSize = 1; } if (pageIndex < 1) { pageIndex = 1; } //开始 string html = string.Empty; string htmlLeftPage = string.Empty; string htmlRightPage = string.Empty; if (pageLink == string.Empty) { pageLink = "?"; } //开始计算 long pageCount = 0; if (recordCount % pageSize == 0) { pageCount = recordCount / pageSize; } else { pageCount = (recordCount / pageSize) + 1; } for (int i = 2; i >= 1; i--) { if (pageIndex - i >= 1) { htmlLeftPage += string.Format("<a href=\"{0}\">{1}</a>", string.Format(pageLink, (pageIndex - i)), pageIndex - i); } } for (int j = 1; j <= 2; j++) { if (pageIndex + j <= pageCount) { htmlRightPage += string.Format("<a href=\"{0}\">{1}</a>", string.Format(pageLink, (pageIndex + j)), pageIndex + j); } } long prevPage = pageIndex - 1; if (prevPage < 1) { prevPage = 1; } long nextPage = pageIndex + 1; if (nextPage > pageCount) { nextPage = pageCount; } //最后处理 if (nextPage < 1) { nextPage = 1; } if (pageCount < 1) { pageCount = 1; } //计算结束 string leftHtml = string.Format("<a {0}>{1}</a><a {2}>{3}</a>{4}", pageIndex != 1 ? string.Format("href=\"{0}\"", string.Format(pageLink, 1)) : "", homePageText, pageIndex != prevPage ? string.Format("href=\"{0}\"", string.Format(pageLink, prevPage)) : "", prevPageText, htmlLeftPage); string rightHtml = string.Format("{0}<a {1}>{2}</a><a {3}>{4}</a>", htmlRightPage, pageIndex != nextPage ? string.Format("href=\"{0}\"", string.Format(pageLink, nextPage)) : "", nextPageText, pageIndex != pageCount ? string.Format("href=\"{0}\"", string.Format(pageLink, pageCount)) : "", lastPageText); html = string.Format("<div class=\"pagerbox\">{0}<b>{1}</b>{2}</div><span>{3}</span>", leftHtml, pageIndex, rightHtml, string.Format(displayMsg, pageIndex, pageCount, recordCount)); return html; }