惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
I
InfoQ
H
Help Net Security
GbyAI
GbyAI
博客园 - 叶小钗
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
Y
Y Combinator Blog
L
LangChain Blog
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
博客园 - Franky
D
Docker
Jina AI
Jina AI
罗磊的独立博客

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
Net Core 实现一个简单的分页功能-zodream梦想开源/个人编程日记
zodream · 2021-04-03 · via zodream梦想开源/个人编程日记

主要代码

    [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

使用

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

        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 查询获取到的分页数据

@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

转载请保留原文链接: https://zodream.cn/blog/id/197.html