asp.net mvc 自定义 pager 封装与优化
Intro
之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自定义分页 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());
}
Pager V3.0
/// <summary>
/// Pager V3.0
/// </summary>
/// <param name="helper">HtmlHelper</param>
/// <param name="pager">分页信息</param>
/// <param name="onPageChange">翻页地址或事件</param>
/// <param name="pagerViewName">分页分部视图名称</param>
/// <param name="displayMode">分页显示模式</param>
/// <returns></returns>
public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager, Func<int, string> onPageChange, string pagerViewName, PagingDisplayMode displayMode = PagingDisplayMode.Always)
{
pager.OnPageChange = onPageChange;
pager.PagingDisplayMode = displayMode;
return MvcHtmlString.Create(helper.Partial(pagerViewName, pager).ToHtmlString());
}
Summary
Pager V1.0 分页样式和翻页事件如果要修改都要修改 CSharp 代码
Pager V2.0 可以自定义翻页事件,但是要修改分页样式还是要修改 CSharp 代码
Pager V3.0 可以在自定义分页分部视图中修改翻页事件或地址和分页样式,可以实现样式数据代码的分离
End
自定义 MvcSimplePager 项目和示例代码:https://github.com/WeihanLi/MvcSimplePager
对于 asp.net core 也是支持的哟 (^U^)ノ~YO