之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自定义分页 pager 越来越容易扩展了。
HtmlHelper Pager扩展
Pager V1.0 :
/// <summary> /// Pager V1.0 /// </summary> /// <param name="helper">HtmlHelper</param> /// <param name="pager">分页信息</param> /// <returns></returns> public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager) { StringBuilder sbHtmlText = new StringBuilder(); sbHtmlText.Append("<div style=/"text-align:center/"><nav><ul class=/"pagination/">"); if (pager.PageIndex <= 1) { sbHtmlText.Append("<li class=/"disabled/"><a href=/"javascript:void(0)/" aria-label=/"Previous/"><span aria-hidden=/"true/">«</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=/"javascript:loadData(1)/" aria-label=/"1/"><span aria-hidden=/"true/">«</span></a></li>", pager.PageIndex - 1); sbHtmlText.AppendFormat("<li><a href=/"javascript:loadData({0})/">{0}</a></li>", pager.PageIndex - 1); } sbHtmlText.AppendFormat("<li class=/"active/"><a href=/"javascript:void(0)/">{0}<span class=/"sr-only/">(current)</span></a></li>", pager.PageIndex); if (pager.PageIndex >= pager.PageCount) { sbHtmlText.Append("<li class=/"disabled/"><a href=/"javascript:void(0)/" aria-label=/"Next/"><span aria-hidden=/"true/">»</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=/"javascript:loadData({0})/">{0}</a></li>", pager.PageIndex + 1); sbHtmlText.AppendFormat("<li><a href=/"javascript:loadData({0})/" aria-label=/"Next/"><span aria-hidden=/"true/">»</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 name="helper">HtmlHelper</param> /// <param name="pager">分页信息</param> /// <param name="onPageChange">翻页事件</param> /// <returns></returns> public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager, Func<int, string> onPageChange) { StringBuilder sbHtmlText = new StringBuilder(); sbHtmlText.Append("<div style=/"text-align:center/"><nav><ul class=/"pagination/">"); if (pager.PageIndex <= 1) { sbHtmlText.Append("<li class=/"disabled/"><a href=/"javascript:void(0)/" aria-label=/"Previous/"><span aria-hidden=/"true/">«</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=/"{0}/" aria-label=/"Previous/"><span aria-hidden=/"true/">«</span></a></li>", onPageChange(pager.PageIndex - 1)); sbHtmlText.AppendFormat("<li><a href=/"{0}/">{1}</a></li>", onPageChange(pager.PageIndex - 1), pager.PageIndex - 1); } sbHtmlText.AppendFormat("<li class=/"active/"><a href=/"javascript:void(0)/">{0}<span class=/"sr-only/">(current)</span></a></li>", pager.PageIndex); if (pager.PageIndex >= pager.PageCount) { sbHtmlText.Append("<li class=/"disabled/"><a href=/"javascript:void(0)/" aria-label=/"Next/"><span aria-hidden=/"true/">»</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=/"{0}/">{1}</a></li>", onPageChange(pager.PageIndex + 1), pager.PageIndex + 1); sbHtmlText.AppendFormat("<li><a href=/"{0}/" aria-label=/"Next/"><span aria-hidden=/"true/">»</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()); }
新闻热点
疑难解答
图片精选