首页 > 编程 > .NET > 正文

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

2024-07-10 12:49:37
字体:
来源:转载
供稿:网友

之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自定义分页 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());  }             
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表