using System;
using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.Mvc;using C2BUY.Community.Entity;using System.Web.Routing;namespace C2BUY.Community.Web.Core.UIHelper
{ public class PagerBuilder { private readonly HtmlHelper _html; private readonly int _totalCount; private readonly int _pageIndex; private readonly int _pageSize; private readonly int _limitMaxPages; private readonly RouteValueDictionary _routeValues; private readonly string _actionName; private readonly string _controllerName; private readonly string _routeName; private readonly string _onSuccess; private readonly string _updateTargetId;public PagerBuilder(HtmlHelper html, int totalCount, int pageIndex, int pageSize, string actionName, string controllerName, string routeName, string updateTargetId, string onSuccess)
{ if (String.IsNullOrEmpty(actionName)) { actionName = (string)html.ViewContext.RouteData.Values["action"]; } if (String.IsNullOrEmpty(controllerName)) { controllerName = (string)html.ViewContext.RouteData.Values["controller"]; }_html = html;
_totalCount = totalCount; _pageIndex = pageIndex; _pageSize = pageSize; _actionName = actionName; _controllerName = controllerName; _routeName = routeName; _updateTargetId = updateTargetId; _onSuccess = onSuccess; }public string RenderPager()
{ //当前页,前面和后面显示的页数 int showNum = 2; //最前面和最后面显示的页数 int lastNum = 2;string endHtml = "";
int pageIndex = _pageIndex;
int pageSize = _pageSize;//总页数
int totalPage = (int)Math.Ceiling((double)((double)_totalCount / (double)_pageSize)); if (totalPage < 1) return ""; if (_limitMaxPages != 0) { if (totalPage > _limitMaxPages) totalPage = _limitMaxPages; endHtml = string.Format(" 共{0}页", totalPage); } else { endHtml = string.Format(" {1}/{2} 共{0}条", _totalCount, pageIndex, totalPage); }if (pageIndex <= 0) pageIndex = 1;
if (pageIndex > totalPage) pageIndex = totalPage;System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<div class=\"pager m_w\">"); int startNum = 0; int endNum = 0;startNum = pageSize * (pageIndex - 1) + 1;
if (totalPage == pageIndex) { endNum = _totalCount; } else { endNum = pageSize * pageIndex; }//前半部分
if (pageIndex > 1) { sb.Append(CreatePagerItem(pageIndex - 1, "上一页", _onSuccess)); }if (pageIndex <= showNum + lastNum + 1)
{ for (int i = 1; i < pageIndex; i++) { sb.AppendFormat(CreatePagerItem(i, i.ToString(), _onSuccess)); } } else { for (int i = 1; i <= lastNum; i++) { sb.AppendFormat(CreatePagerItem(i, i.ToString(), _onSuccess)); }sb.Append("…");
for (int i = pageIndex - showNum; i < pageIndex; i++)
{ sb.AppendFormat(CreatePagerItem(i, i.ToString(), _onSuccess)); } }//当前页
if (!(pageIndex == 1 && totalPage == 1)) sb.Append("<span>" + pageIndex.ToString() + "</span>");//后半部分
for (int i = pageIndex + 1; i <= pageIndex + showNum && i <= totalPage; i++) { sb.Append(CreatePagerItem(i, i.ToString(), _onSuccess)); }if (pageIndex + showNum + lastNum < totalPage)
{ sb.Append("…");for (int i = totalPage - lastNum + 1; i <= totalPage; i++)
{ sb.Append(CreatePagerItem(i, i.ToString(), _onSuccess)); } } else { for (int i = pageIndex + showNum + 1; i <= totalPage; i++) { sb.Append(CreatePagerItem(i, i.ToString(), _onSuccess)); } }if (pageIndex < totalPage)
{ sb.Append(CreatePagerItem(pageIndex + 1, "下一页", _onSuccess)); } sb.Append(endHtml); sb.Append("</div>"); return sb.ToString(); }private string CreatePagerItem(int pageIndex, string text, string OnSuccess)
{ string linkUrl = GenerateUrl(pageIndex); if (string.IsNullOrEmpty(_updateTargetId)) { return string.Format("<a href=\"{0}\">{1}</a>", linkUrl, text); //return "<a href=\"" + GenerateUrl(pageIndex) + "\">" + text + "</a>"; } if (string.IsNullOrEmpty(OnSuccess)) { return string.Format("<a href=\"{0}\" page-ajax=\"true\" page-update=\"#{1}\">{2}</a>", linkUrl, _updateTargetId, text); } return string.Format("<a href=\"{0}\" page-ajax=\"true\" page-update=\"#{1}\" page-ajax-success=\"" + OnSuccess + "\">{2}</a>", linkUrl, _updateTargetId, text);//return "<a page-ajax=\"true\" href=\"" + GenerateUrl(pageIndex) + "\" οnclick=\"$('#" + _updateTargetId + "').load($(this).attr('href') #" + _updateTargetId + ");return false;\">" + text + "</a>";
}private string CreatePagerItem(int pageIndex, string text)
{ return CreatePagerItem(pageIndex, text, null); }private string GenerateUrl(int pageIndex)
{ var routeValues = GetCurrentRouteValues(_html.ViewContext); // _routeValues??new RouteValueDictionary();routeValues["pi"] = pageIndex;
UrlHelper urlHelper = new UrlHelper(_html.ViewContext.RequestContext);
if (!string.IsNullOrEmpty(_routeName)) return urlHelper.RouteUrl(_routeName, routeValues); return urlHelper.RouteUrl(routeValues); }private RouteValueDictionary GetCurrentRouteValues(ViewContext viewContext)
{ var routeValues = _routeValues ?? new RouteValueDictionary(); var rq = viewContext.HttpContext.Request.QueryString; if (rq != null && rq.Count > 0) { var invalidParams = new[] { "x-requested-with", "xmlhttprequest", "pi" }; foreach (string key in rq.Keys) { // 添加url参数到路由中 if (!string.IsNullOrEmpty(key) && Array.IndexOf(invalidParams, key.ToLower()) < 0) { routeValues[key] = rq[key]; } } } // action routeValues["action"] = _actionName; // controller routeValues["controller"] = _controllerName; return routeValues; } }}
using System;
using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.Mvc;using C2BUY.Community.Entity;namespace C2BUY.Community.Web.Core.UIHelper
{ public static class PagerHelper { /// <summary> /// 生成URL分页效果 /// </summary> /// <param name="html"></param> /// <param name="pagedList">PagedList实体对象</param> /// <returns></returns> public static MvcHtmlString Pager(this HtmlHelper html, IPagedList pagedList) { PagerBuilder pager = new PagerBuilder(html, pagedList.TotalCount, pagedList.PageIndex, pagedList.PageSize, null, null, null, null, null); return MvcHtmlString.Create(pager.RenderPager()); }/// <summary>
/// 生成URL分页效果(重新指定Action和Controller) /// </summary> /// <param name="html"></param> /// <param name="pagedList">PagedList实体对象</param> /// <param name="actionName">Action名称</param> /// <param name="controllerName">Controller名称</param> /// <returns></returns> public static MvcHtmlString Pager(this HtmlHelper html, IPagedList pagedList, string actionName, string controllerName) { PagerBuilder pager = new PagerBuilder(html, pagedList.TotalCount, pagedList.PageIndex, pagedList.PageSize, actionName, controllerName, null, null, null); return MvcHtmlString.Create(pager.RenderPager()); }/// <summary>
/// 生成Ajax分页效果(重新指定Action和Controller) /// </summary> /// <param name="html"></param> /// <param name="pagedList">PagedList实体对象</param> /// <param name="actionName">Action名称</param> /// <param name="controllerName">Controller名称</param> /// <param name="updateTargetId">需要更新部分的ID</param> /// <param name="onSuccess">更新完成后执行的回调函数</param> /// <returns></returns> public static MvcHtmlString AjaxPager(this HtmlHelper html, IPagedList pagedList, string actionName, string controllerName, string updateTargetId, string onSuccess) { PagerBuilder pager = new PagerBuilder(html, pagedList.TotalCount, pagedList.PageIndex, pagedList.PageSize, actionName, controllerName, null, updateTargetId, onSuccess); return MvcHtmlString.Create(pager.RenderPager()); } /// <summary> /// 生成Ajax分页效果(重新指定Action和Controller) /// </summary> /// <param name="html"></param> /// <param name="pagedList">PagedList实体对象</param> /// <param name="actionName">Action名称</param> /// <param name="controllerName">Controller名称</param> /// <param name="updateTargetId">需要更新部分的ID</param> /// <returns></returns> public static MvcHtmlString AjaxPager(this HtmlHelper html, IPagedList pagedList, string actionName, string controllerName, string updateTargetId) { return AjaxPager(html, pagedList, actionName, controllerName, updateTargetId, null); }/// <summary>
/// 生成Ajax分页效果 /// </summary> /// <param name="html"></param> /// <param name="pagedList">PagedList实体对象</param> /// <param name="updateTargetId">需要更新部分的ID</param> /// <param name="onSuccess">更新完成后执行的回调函数</param> /// <returns></returns> public static MvcHtmlString AjaxPager(this HtmlHelper html, IPagedList pagedList, string updateTargetId, string onSuccess) { PagerBuilder pager = new PagerBuilder(html, pagedList.TotalCount, pagedList.PageIndex, pagedList.PageSize, null, null, null, updateTargetId, onSuccess); return MvcHtmlString.Create(pager.RenderPager()); }/// <summary>
/// 生成Ajax分页效果 /// </summary> /// <param name="html"></param> /// <param name="pagedList">PagedList实体对象</param> /// <param name="updateTargetId">需要更新部分的ID</param> /// <returns></returns> public static MvcHtmlString AjaxPager(this HtmlHelper html, IPagedList pagedList, string updateTargetId) { return AjaxPager(html, pagedList, updateTargetId, null); } }}