asp.net mvc 自定义pager封装与优化
Intro
之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自定义分页 pager 越来越容易扩展了。
HtmlHelper Pager扩展
Pager V1.0 :
1 /// <summary> 2 /// Pager V1.0 3 /// </summary> 4 /// <param name="helper">HtmlHelper</param> 5 /// <param name="pager">分页信息</param> 6 /// <returns></returns> 7 public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager) 8 { 9 StringBuilder sbHtmlText = new StringBuilder(); 10 sbHtmlText.Append("<div style="text-align:center"><nav><ul class="pagination">"); 11 if (pager.PageIndex <= 1) 12 { 13 sbHtmlText.Append("<li class="disabled"><a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">«</span></a></li>"); 14 } 15 else 16 { 17 sbHtmlText.AppendFormat("<li><a href="javascript:loadData(1)" aria-label="1"><span aria-hidden="true">«</span></a></li>", pager.PageIndex - 1); 18 sbHtmlText.AppendFormat("<li><a href="javascript:loadData({0})">{0}</a></li>", pager.PageIndex - 1); 19 } 20 sbHtmlText.AppendFormat("<li class="active"><a href="javascript:void(0)">{0}<span class="sr-only">(current)</span></a></li>", pager.PageIndex); 21 if (pager.PageIndex >= pager.PageCount) 22 { 23 sbHtmlText.Append("<li class="disabled"><a href="javascript:void(0)" aria-label="Next"><span aria-hidden="true">»</span></a></li>"); 24 } 25 else 26 { 27 sbHtmlText.AppendFormat("<li><a href="javascript:loadData({0})">{0}</a></li>", pager.PageIndex + 1); 28 sbHtmlText.AppendFormat("<li><a href="javascript:loadData({0})" aria-label="Next"><span aria-hidden="true">»</span></a></li>", pager.PageIndex + 1); 29 } 30 sbHtmlText.Append("</ul></nav>"); 31 sbHtmlText.AppendFormat("<div><span>每页有<strong>{0}</strong>条数据,一共有<strong>{1}</strong>页,总计<strong>{2}</strong>条数据</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); 32 return MvcHtmlString.Create(sbHtmlText.ToString()); 33 }
Pager V2.0
1 /// <summary> 2 /// Pager V2.0 3 /// </summary> 4 /// <param name="helper">HtmlHelper</param> 5 /// <param name="pager">分页信息</param> 6 /// <param name="onPageChange">翻页事件</param> 7 /// <returns></returns> 8 public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager, Func<int, string> onPageChange) 9 { 10 StringBuilder sbHtmlText = new StringBuilder(); 11 sbHtmlText.Append("<div style="text-align:center"><nav><ul class="pagination">"); 12 if (pager.PageIndex <= 1) 13 { 14 sbHtmlText.Append("<li class="disabled"><a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">«</span></a></li>"); 15 } 16 else 17 { 18 sbHtmlText.AppendFormat("<li><a href="{0}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>", onPageChange(pager.PageIndex - 1)); 19 sbHtmlText.AppendFormat("<li><a href="{0}">{1}</a></li>", onPageChange(pager.PageIndex - 1), pager.PageIndex - 1); 20 } 21 sbHtmlText.AppendFormat("<li class="active"><a href="javascript:void(0)">{0}<span class="sr-only">(current)</span></a></li>", pager.PageIndex); 22 if (pager.PageIndex >= pager.PageCount) 23 { 24 sbHtmlText.Append("<li class="disabled"><a href="javascript:void(0)" aria-label="Next"><span aria-hidden="true">»</span></a></li>"); 25 } 26 else 27 { 28 sbHtmlText.AppendFormat("<li><a href="{0}">{1}</a></li>", onPageChange(pager.PageIndex + 1), pager.PageIndex + 1); 29 sbHtmlText.AppendFormat("<li><a href="{0}" aria-label="Next"><span aria-hidden="true">»</span></a></li>", onPageChange(pager.PageIndex + 1)); 30 } 31 sbHtmlText.Append("</ul></nav>"); 32 sbHtmlText.AppendFormat("<div><span>每页有<strong>{0}</strong>条数据,一共有<strong>{1}</strong>页,总计<strong>{2}</strong>条数据</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); 33 return MvcHtmlString.Create(sbHtmlText.ToString()); 34 }
Pager V3.0
1 /// <summary> 2 /// Pager V3.0 3 /// </summary> 4 /// <param name="helper">HtmlHelper</param> 5 /// <param name="pager">分页信息</param> 6 /// <param name="onPageChange">翻页地址或事件</param> 7 /// <param name="pagerViewName">分页分部视图名称</param> 8 /// <param name="displayMode">分页显示模式</param> 9 /// <returns></returns> 10 public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager, Func<int, string> onPageChange, string pagerViewName, PagingDisplayMode displayMode = PagingDisplayMode.Always) 11 { 12 pager.OnPageChange = onPageChange; 13 pager.PagingDisplayMode = displayMode; 14 return MvcHtmlString.Create(helper.Partial(pagerViewName, pager).ToHtmlString()); 15 }
Summary
Pager V1.0 分页样式和翻页事件如果要修改都要修改 CSharp 代码
Pager V2.0 可以自定义翻页事件,但是要修改分页样式还是要修改 CSharp 代码
Pager V3.0 可以在自定义分页分部视图中修改翻页事件或地址和分页样式
End
现在已经将这个项目 MvcSimplePager 打包到 Nuget http://www.nuget.org/packages/MvcSimplePager/,你可以在VS中 nuget 包管理器中搜索 MvcSimplePager 进行使用。
自定义 MvcSimplePager 项目和示例代码:https://github.com/WeihanLi/MvcSimplePager ,你可以根据需要再进行自己的定制。