asp.net mvc自定义pager封装与优化

之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自定义分页 pager 越来越容易扩展了。

HtmlHelper Pager扩展

Pager V1.0 :

/// <summary> /// Pager V1.0 /// </summary> /// <param>HtmlHelper</param> /// <param>分页信息</param> /// <returns></returns> public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager) { StringBuilder sbHtmlText = new StringBuilder(); sbHtmlText.Append("<div style=https://www.jb51.net/article/\"text-align:center\"><nav><ul class=https://www.jb51.net/article/\"pagination\">"); if (pager.PageIndex <= 1) { sbHtmlText.Append("<li class=https://www.jb51.net/article/\"disabled\"><a href=https://www.jb51.net/article/\"javascript:void(0)\" aria-label=https://www.jb51.net/article/\"Previous\"><span aria-hidden=https://www.jb51.net/article/\"true\">&laquo;</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"javascript:loadData(1)\" aria-label=https://www.jb51.net/article/\"1\"><span aria-hidden=https://www.jb51.net/article/\"true\">&laquo;</span></a></li>", pager.PageIndex - 1); sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"javascript:loadData({0})\">{0}</a></li>", pager.PageIndex - 1); } sbHtmlText.AppendFormat("<li class=https://www.jb51.net/article/\"active\"><a href=https://www.jb51.net/article/\"javascript:void(0)\">{0}<span class=https://www.jb51.net/article/\"sr-only\">(current)</span></a></li>", pager.PageIndex); if (pager.PageIndex >= pager.PageCount) { sbHtmlText.Append("<li class=https://www.jb51.net/article/\"disabled\"><a href=https://www.jb51.net/article/\"javascript:void(0)\" aria-label=https://www.jb51.net/article/\"Next\"><span aria-hidden=https://www.jb51.net/article/\"true\">&raquo;</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"javascript:loadData({0})\">{0}</a></li>", pager.PageIndex + 1); sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"javascript:loadData({0})\" aria-label=https://www.jb51.net/article/\"Next\"><span aria-hidden=https://www.jb51.net/article/\"true\">&raquo;</span></a></li>", pager.PageIndex + 1); } sbHtmlText.Append("</ul></nav>"); sbHtmlText.AppendFormat("<div><span>每页有<strong>{0}</strong>条数据,一共有<strong>{1}</strong>页,总计<strong>{2}</strong>条数据</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); return MvcHtmlString.Create(sbHtmlText.ToString()); }

Pager V2.0

/// <summary> /// Pager V2.0 /// </summary> /// <param>HtmlHelper</param> /// <param>分页信息</param> /// <param>翻页事件</param> /// <returns></returns> public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager, Func<int, string> onPageChange) { StringBuilder sbHtmlText = new StringBuilder(); sbHtmlText.Append("<div style=https://www.jb51.net/article/\"text-align:center\"><nav><ul class=https://www.jb51.net/article/\"pagination\">"); if (pager.PageIndex <= 1) { sbHtmlText.Append("<li class=https://www.jb51.net/article/\"disabled\"><a href=https://www.jb51.net/article/\"javascript:void(0)\" aria-label=https://www.jb51.net/article/\"Previous\"><span aria-hidden=https://www.jb51.net/article/\"true\">&laquo;</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"{0}\" aria-label=https://www.jb51.net/article/\"Previous\"><span aria-hidden=https://www.jb51.net/article/\"true\">&laquo;</span></a></li>", onPageChange(pager.PageIndex - 1)); sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"{0}\">{1}</a></li>", onPageChange(pager.PageIndex - 1), pager.PageIndex - 1); } sbHtmlText.AppendFormat("<li class=https://www.jb51.net/article/\"active\"><a href=https://www.jb51.net/article/\"javascript:void(0)\">{0}<span class=https://www.jb51.net/article/\"sr-only\">(current)</span></a></li>", pager.PageIndex); if (pager.PageIndex >= pager.PageCount) { sbHtmlText.Append("<li class=https://www.jb51.net/article/\"disabled\"><a href=https://www.jb51.net/article/\"javascript:void(0)\" aria-label=https://www.jb51.net/article/\"Next\"><span aria-hidden=https://www.jb51.net/article/\"true\">&raquo;</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"{0}\">{1}</a></li>", onPageChange(pager.PageIndex + 1), pager.PageIndex + 1); sbHtmlText.AppendFormat("<li><a href=https://www.jb51.net/article/\"{0}\" aria-label=https://www.jb51.net/article/\"Next\"><span aria-hidden=https://www.jb51.net/article/\"true\">&raquo;</span></a></li>", onPageChange(pager.PageIndex + 1)); } sbHtmlText.Append("</ul></nav>"); sbHtmlText.AppendFormat("<div><span>每页有<strong>{0}</strong>条数据,一共有<strong>{1}</strong>页,总计<strong>{2}</strong>条数据</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); return MvcHtmlString.Create(sbHtmlText.ToString()); }

Pager V3.0

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjydwp.html