注:自己的代码收藏,对各位或许没有用。
1) 拓展 HtmlHelper 类
public static class HtmlHelperEx
{
private const string AnchorFormat = "<a class=\"GridPager_Page\" href=\"?{0}&pageIndex={1}\">{2}</a>";
private const string PlaceHolderFormat = "<span class=\"GridPager_Page\">{0}</span>";
private const string HiddenPage = "...";
private const string BeginDiv = "<div class=\"GridPager\">";
private const string EndDiv = "</div>";
private const string PrevPageText = "<<Prev";
private const string NextPageText = "Next>>";
private const int MAX_PAGE_DISPLAY = 10;// 1,2,3,4,5,6,7,8,9,10
private const int PAGE_COUNT_NEWXT_TO_CURRENT_PAGE = 2;// 1,2 ..(5,6),[7],(8,9)..20,10
private const int PAGE_COUNT_AT_END = 2;// (1,2) ..5,6,[7],8,9..(20,21)
public static MvcHtmlString GridPager(this HtmlHelper htmlHelper)
{
StringBuilder html = new StringBuilder(BeginDiv);
string queryStr = htmlHelper.ViewBag.QueryString;
int pageIndex = htmlHelper.ViewBag.PageIndex;
int pageCount = htmlHelper.ViewBag.PageCount;
int nextPage = pageIndex;
if (nextPage < 1) { nextPage = 1; }
if (nextPage > pageCount) { nextPage = pageCount; }
if (nextPage > 1)
{
html.AppendFormat(AnchorFormat, queryStr, nextPage - 1, PrevPageText);
}
if (pageCount > MAX_PAGE_DISPLAY)
{
AddAnchor(html, nextPage, 1, PAGE_COUNT_AT_END, queryStr);
if (nextPage - PAGE_COUNT_NEWXT_TO_CURRENT_PAGE > PAGE_COUNT_AT_END + 2)
{
// 1,2 ... 5,6,[7]
int beginPage = nextPage - PAGE_COUNT_NEWXT_TO_CURRENT_PAGE;
if (nextPage + PAGE_COUNT_NEWXT_TO_CURRENT_PAGE * 2 >= pageCount)
{
// 1,2 .. 13,14,15,16,17,18,19,20
beginPage = pageCount - (MAX_PAGE_DISPLAY - PAGE_COUNT_AT_END) + 1;
}
if (beginPage <= PAGE_COUNT_AT_END)
{
beginPage = PAGE_COUNT_AT_END + 1;
}
html.AppendFormat(PlaceHolderFormat, HiddenPage);
AddAnchor(html, nextPage, beginPage, nextPage, queryStr);
}
else
{
// 1,2,3,4,5,[6]
AddAnchor(html, nextPage, PAGE_COUNT_AT_END + 1, nextPage, queryStr);
}
if (pageCount - PAGE_COUNT_AT_END > nextPage + PAGE_COUNT_NEWXT_TO_CURRENT_PAGE + 1)
{
// [6],7,8 .. 11,12
int tillPage = nextPage + PAGE_COUNT_NEWXT_TO_CURRENT_PAGE;
if (tillPage < MAX_PAGE_DISPLAY - PAGE_COUNT_AT_END)
{
// 1,2,3,4,5,6,7,8 .. 19,20
tillPage = MAX_PAGE_DISPLAY - PAGE_COUNT_AT_END;
}
AddAnchor(html, nextPage, nextPage == 1 ? PAGE_COUNT_AT_END + 1 : nextPage + 1, tillPage, queryStr);
html.AppendFormat(PlaceHolderFormat, HiddenPage);
AddAnchor(html, nextPage, pageCount - 1, pageCount, queryStr);
}
else
{
// [6],7,8,9,10,11
AddAnchor(html, nextPage, nextPage + 1, pageCount, queryStr);
}
}
else
{
if (pageCount > 1)
{
AddAnchor(html, nextPage, 1, pageCount, queryStr);
}
}
if (pageCount - nextPage > 0)
{
html.AppendFormat(AnchorFormat, queryStr, nextPage + 1, NextPageText);
}
return new MvcHtmlString(html.Append(EndDiv).ToString());
}
private static void AddAnchor(StringBuilder html, int current, int from, int to, string queryStr)
{
for (int i = from; i <= to; i++)
{
if (current == i)
{
html.AppendFormat(PlaceHolderFormat, i);
continue;
}
html.AppendFormat(AnchorFormat, queryStr, i, i);
}
}
}
2) 定义自己的ControllerBase 来辅助完成一些功能
public class GridControllerBase : DataControllerBase
{
private int pageIndex = 1;
private int pageCount = 0;
protected char DateDelimiter = '~';
/// <summary>
/// Current page index(begin with 1)
/// </summary>
protected int PageIndex
{
get
{
// the final adjustment.
if (this.pageIndex > this.pageCount)
{
return this.pageCount;
}
return pageIndex;
}
}
/// <summary>
/// The total page count.
/// </summary>
protected int PageCount
{
get { return pageCount; }
set { pageCount = value; }
}
/// <summary>
/// Page Size.
/// </summary>
protected int PageSize
{
get { return 10; }
}
public string SearchKey { get; set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
int.TryParse(Request.QueryString["pageIndex"] ?? "1", out this.pageIndex);
if (this.pageIndex < 1)
{
this.pageIndex = 1;
}
this.SearchKey = Request.QueryString["searchKey"];
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
ViewBag.PageIndex = this.pageIndex > this.pageCount ? this.pageCount : this.pageIndex;
ViewBag.PageCount = this.pageCount;
ViewBag.QueryString = "searchKey=" + this.SearchKey;
ViewBag.SearchKey = this.SearchKey;
}
/// <summary>
/// Evaluate page count.
/// </summary>
/// <param name="totalCount">Total record count.</param>
/// <returns></returns>
protected int GetPageCount(int totalCount)
{
return (totalCount + this.PageSize - 1) / this.PageSize;
}
}
3) 用法
@if (Model == null || Model.Count() == 0)
{
<div>
No data, please refine your search.
</div>
}
else
{
@Html.GridPager()
}
@if (Model == null || Model.Count() == 0)
{
<div>
No data, please refine your search.
</div>
}
else
{
@Html.GridPager()
}