博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分页笔记
阅读量:5127 次
发布时间:2019-06-13

本文共 8913 字,大约阅读时间需要 29 分钟。

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("&nbsp;&nbsp;共{0}页", totalPage);
}
else
{
endHtml = string.Format("&nbsp;&nbsp;{1}/{2}&nbsp;&nbsp;共{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);
}
}
}

转载于:https://www.cnblogs.com/miao-park/archive/2012/11/28/2793077.html

你可能感兴趣的文章
微信 oauth2 两次回调
查看>>
洛谷P1099 树网的核
查看>>
Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
查看>>
接口测试用例设计
查看>>
WebConfig配置文件详解(转载自逆心的博客)
查看>>
Ex3_28 在2SAT问题中,给定一个字句的集合..._第十二次作业
查看>>
如何截取iframe的内容,修改他的CSS
查看>>
telnet测试端口是否正常打开
查看>>
python中的__new__、__init__和__del__
查看>>
PHP使用缓存提高网站性能
查看>>
用C#实现智能设备上的NotifyIcon类
查看>>
项目实施(二)
查看>>
HDU 1045 Fire Net
查看>>
Github
查看>>
cmake 手册详解【转】
查看>>
一般在页面上添加隐藏域用来接受设置一些值方便开发
查看>>
net 表格控件
查看>>
CodeForces Round 197 Div2
查看>>
boost-使用format和lexical_cast实现数字和字符串之间的转换
查看>>
Learn a Linux command every day--day2:ls命令
查看>>