Net Core 实现一个简单的分页功能

Fork Me On Github

主要代码

c#
                                                                                                               
    [HtmlTargetElement("pagination")]
    public class PagerTagHelper : TagHelper
    {
        // 总共有多少条记录
        public long Total { get; set; } = 0;

        // 一页有多少条记录
        public int PerPage { get; set; } = 20;

        // 当前第几页
        public int Page { get; set; } = 1;

        // 分页链接的最多显示个数
        public int PageLength { get; set; } = 7;

        private string url;
        // 当前网址
        public string Url
        {
            get { return url; }
            set {
                if (value.IndexOf('?') < 0)
                {
                    url = value + "?page=";
                    return;
                }
                url = Regex.Replace(value, @"([\?\&])page=\d+\&*", "$1") + "&page=";
            }
        }

        // 是否显示上一页下一页
        public bool DirectionLinks { get; set; } = false;

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "div";
            var html = new StringBuilder();
            var items = initLinks(out bool canPrevious, out bool canNext);
            html.Append("<ul class=\"pagination\">");
            if (DirectionLinks && canPrevious)
            {
                html.AppendFormat("<li class=\"page-item{0}\"><a class=\"page-link\" href=\"{1}{2}\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</span><span class=\"sr-only\">上一页</span></a></li>", 
                    canPrevious ? "" : " disabled",
                    Url, Page - 1
                    );
            }
            foreach (var item in items)
            {
                if (item < 1)
                {
                    html.Append("<li class=\"page-item disabled\"><a class=\"page-link\">...</a></li>");
                    continue;
                }
                html.AppendFormat("<li class=\"page-item{0}\"><a class=\"page-link\" href=\"{2}{3}\">{1}</a></li>",
                    item == Page ? " active" : "",
                    item,
                    Url, item
                    );
            }
            if (DirectionLinks && canNext)
            {
                html.AppendFormat("<li class=\"page-item{0}\"><a class=\"page-link\" href=\"{1}{2}\"  aria-label=\"Next\"><span aria-hidden=\"true\">&raquo;</span><span class=\"sr-only\">下一页</span></a></li>",
                    canNext ? "" : " disabled",
                    Url, Page + 1
                    );
            }
            html.Append("</ul>");
            output.Content.SetHtmlContent(html.ToString());
        }

        private List<int> initLinks(out bool canPrevious, out bool canNext)
        {
            var total = (int)Math.Ceiling((double)Total / PerPage);
            canPrevious = Page > 1;
            canNext = Page < total;
            var items = new List<int>();
            if (total < 2)
            {
                return items;
            }
            items.Add(1);
            var lastList = (int)Math.Floor((double)PageLength / 2);
            var i = Page - lastList;
            var length = Page + lastList;
            if (i < 2)
            {
                i = 2;
                length = i + PageLength;
            }
            if (length > total - 1)
            {
                length = total - 1;
                i = Math.Max(2, length - PageLength);
            }

            if (i > 2)
            {
                items.Add(0);
            }
            for (; i <= length; i++)
            {
                items.Add(i);
            }
            if (length < total - 1)
            {
                items.Add(0);
            }
            items.Add(total);
            return items;
        }
    }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111

使用

这是控制器中的方法,需要传递当前网址到分页类中。

c#
       
        public IActionResult Index(int page = 1)
        {
            ViewData["items"] = _repository.GetPage(page);
            ViewData["fullUrl"] = $"{HttpContext.Request.Path}{HttpContext.Request.QueryString}";
            ViewData["pageIndex"] = page;
            return View();
        }
1234567

NetDream.Web 为当前项目命名空间

PageNPoco 查询获取到的分页数据

html
          
@using NPoco;
@addTagHelper *, NetDream.Web
@{
    var items = ViewData["items"] as Page<BlogModel>;
    var pageIndex = (int)ViewData["pageIndex"];
}


<pagination url="@ViewData["fullUrl"]" total="@items.TotalItems" page="@pageIndex"></pagination>
12345678910
点击查看全文
0 192 0