码迷,mamicode.com
首页 > Web开发 > 详细

core mvc 分页

时间:2019-06-25 17:13:33      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:display   sea   his   float   led   abs   使用   build   bootstra   

看了下XPAGE感觉还是用的不太爽,自己写了个,样式是bootstrap的,需要的小伙伴拿走吧。

public static IHtmlContent Pager(this IHtmlHelper html, long totalCount, int pageSize, string pageStr)
        {
            if (totalCount < 1) return new HtmlString(string.Empty);

            var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); //总页数

            var dicRoute = html.ViewContext.RouteData.Values;
            var strQuery = html.ViewContext.HttpContext.Request.Query;
            int pageIndex;
            if (dicRoute.ContainsKey(pageStr))
            {
                if (!int.TryParse(dicRoute[pageStr].ToString(), out pageIndex)) pageIndex = 1;
            }
            else
            {
                if (!int.TryParse(strQuery[pageStr].ToString(), out pageIndex)) pageIndex = 1;
            }

            if (pageIndex < 1) pageIndex = 1;
            if (pageIndex > totalPages) pageIndex = totalPages;

            foreach (var key in dicRoute.Keys)
            {
                if (string.IsNullOrEmpty(key) || !strQuery.ContainsKey(key)) continue;
                if (!strQuery.TryGetValue(key, out var values)) continue;
                if (values.Count == 1 && !string.IsNullOrEmpty(values))
                {
                    dicRoute[key] = strQuery[key];
                }
            }

            var sbOut = new StringBuilder();
            sbOut.Append("<nav aria-label=\"Page navigation\">");
            sbOut.Append("<ul class=\"pagination\">");
            if (totalPages > 1)
            {
                sbOut.Append("<li");
                sbOut.Append(pageIndex == 1 ? "  class=\"disabled\">" : ">");
                dicRoute[pageStr] = 1;
                sbOut.Append(pageIndex == 1 ? "<a>首页</a>" : GetString(html.RouteLink("首页", dicRoute)));
                sbOut.Append("</li>");


                var pre = pageIndex - 1;
                if (pre < 1) pre = 1;
                sbOut.Append("<li");
                sbOut.Append(pageIndex == 1 ? "  class=\"disabled\">" : ">");
                dicRoute[pageStr] = pre;
                sbOut.Append(pageIndex == 1 ? "<a>上一页</a>" : GetString(html.RouteLink("上一页", dicRoute)));
                sbOut.Append("</li>");


                var nex = pageIndex + 1;
                if (nex > totalPages) nex = totalPages;
                sbOut.Append("<li");
                sbOut.Append(pageIndex == totalPages ? "  class=\"disabled\">" : ">");
                dicRoute[pageStr] = nex;
                sbOut.Append(pageIndex == totalPages ? "<a>下一页</a>" : GetString(html.RouteLink("下一页", dicRoute)));
                sbOut.Append("</li>");

                sbOut.Append("<li");
                sbOut.Append(pageIndex == totalPages ? "  class=\"disabled\">" : ">");
                dicRoute[pageStr] = totalPages;
                sbOut.Append(pageIndex == totalPages ? "<a>尾页</a>" : GetString(html.RouteLink("尾页", dicRoute)));
                sbOut.Append("</li>");
            }

            sbOut.AppendFormat("<li><a>共{0}条数据</a></li>", totalCount);

            sbOut.Append("</ul>");
            sbOut.Append("</nav>");


            return new HtmlString(sbOut.ToString());

        }

说明下,totalCount是总条数,pageSize是每页条数,pageStr是页参数的 key 如:http://123.com/?page=4   pageStr就是 page

使用如下

@Html.PagerNum(10, 1, "page")

 

网络释义
totalcount: 总记录
TotalCount Integer: 伸缩规则总数
TotalCount String: 列表条条目数

core mvc 分页

标签:display   sea   his   float   led   abs   使用   build   bootstra   

原文地址:https://www.cnblogs.com/jzz228/p/11083557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!