【MVC分页】页码
来源:互联网 发布:大数据报表展示平台 编辑:程序博客网 时间:2024/04/30 10:37
注:自己的代码收藏,对各位或许没有用。
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()}
- 【MVC分页】页码
- 分页页码
- 分页页码
- 分页:页码显示javascript
- GridView分页页码
- 仿网页分页页码
- 分页页码显示算法
- 分页页码显示算法
- js分页页码
- 分页页码制作
- 前端分页拼接页码
- 分页页码制作
- jquery实现分页页码
- 分页页码计算
- js分页页码
- datatable指定页码分页
- 分页参数 的页码
- GridView实现数字页码分页
- Linux下SSH使用rsa认证方式省去输入密码
- 微信公众平台 php自动回复时中文乱码
- 微信控 控微信
- 标准I/O限制
- android扫雷程序
- 【MVC分页】页码
- ASCII对照表
- debian,Ubuntu 使用 xrandr 调整分辨率
- SEO需要每天维护网站哪些内容!?
- 【win7常用的15个快捷键组合一定要学会】
- android实现图片缩放 旋转的几种方法
- CURL多线程的严重错误
- mysql 查看表的类型
- java中字符串 字符数组相互转换