• .NET 分页帮助类


    一直以来,分页是一个非常常见的需求,以前也用过很多的分页控件,比如AspNetPager,现在自己参照之前用过的,自己写了一个非常简单实用的分页实现方法。
    先来看看应用效果:
    在这里插入图片描述
    在这里插入图片描述
    如图,定义了是前10页,那么第11页及以后的用...显示,如果是第11页~20页,隐藏前10页以及20以后的内容。
    代码非常简单,也在实际项目中运用了。需要注意的是,分页代码没有参与数据库的数据读取。需要先取出数据来,再设置分页就可以了。

    定义分页基本字段

    private int _currPageIndex;
    private int _pageSize = 25;
    private string _prevPageText = "上页";
    private string _nextPageText = "下页";
    private string _firstPageText = "首页";
    private string _lasePageText = "末页";
    private bool _showFirstLast = true;
    private int _recordCount = 0;
    private int _showNum = 10;
    private string _class = "pagination";
    private bool _ShowMoreButtons = true;
    private bool _ShowPageIndex = true;
    
    /// 
    /// 上一页文字
    /// 
    public string PrevPageText
    {
        get
        {
            return _prevPageText;
        }
        set
        {
            _prevPageText = value;
        }
    }
    /// 
    /// 下一页文字
    /// 
    public string NextPageText
    {
        get
        {
            return _nextPageText;
        }
        set
        {
            _nextPageText = value;
        }
    }
    /// 
    /// 第一页文字
    /// 
    public string FirstPageText
    {
        get
        {
            return _firstPageText;
        }
        set
        {
            _firstPageText = value;
        }
    }
    /// 
    /// 最后一页文字
    /// 
    public string LastPageText
    {
        get
        {
            return _lasePageText;
        }
        set
        {
            _lasePageText = value;
        }
    }
    /// 
    /// 是否显示第一页和最后一页
    /// 
    public bool ShowFirstLast
    {
        get { return _showFirstLast; }
        set { _showFirstLast = value; }
    }
    /// 
    /// 记录数
    /// 
    public int RecordCount
    {
        get { return _recordCount; }
        set { _recordCount = value; }
    }
    /// 
    /// 页大小
    /// 
    public int PageSize
    {
        get { return _pageSize; }
        set { _pageSize = value; }
    }
    /// 
    /// 分多少页
    /// 
    public int PageCount
    {
        get
        {
            if (RecordCount == 0)
            {
                return 1;
            }
            return (int)Math.Ceiling((double)RecordCount / (double)PageSize);
        }
    }
    /// 
    /// 当前页
    /// 
    public int CurrentPageIndex
    {
        set
        {
            _currPageIndex = value;
            if (_currPageIndex < 1)
            {
                _currPageIndex = 1;
            }
        }
        get
        {
    
            if (_currPageIndex > PageCount && PageCount > 0)
            {
                _currPageIndex = PageCount;
            }
            if (_currPageIndex < 1)
            {
                _currPageIndex = 1;
            }
            return _currPageIndex;
        }
    
    }
    /// 
    /// 当前页的URL地址
    /// 
    public string PageURL { get; set; }
    /// 
    /// 页导航或页跳转显示链接数
    /// 
    public int ShowNum
    {
        get { return _showNum; }
        set { _showNum = value; }
    }
    /// 
    /// 分页样式
    /// 
    public string Class
    {
        get { return _class; }
        set { _class = value; }
    }
    /// 
    /// 是否在页导航元素中显示更多页按钮。
    /// 
    public bool ShowMoreButtons
    {
        get { return _ShowMoreButtons; }
        set { _ShowMoreButtons = value; }
    }
    /// 
    /// 是否在页导航元素中显示页索引数值按钮。
    /// 
    public bool ShowPageIndex
    {
        get { return _ShowPageIndex; }
        set { _ShowPageIndex = value; }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171

    基本字段设置内容比较多,大部分都有默认值了。可以不用管,下面介绍分页方法

    分页方法

    public string pager()
    {
        string pageURL = PageURL;
               
    
        int totalPageCount = PageCount;//num3 共多少页
        int pageIndex = CurrentPageIndex;//num 当前页码
        int NumericButtonCount = ShowNum;
        string PageControl = string.Empty;
        //显示页导航元素
        string startMoreButtons = string.Empty;
        string endMoreButtons = string.Empty;
        int startIndex = ((CurrentPageIndex - 1) / NumericButtonCount) * NumericButtonCount;
        int endIndex = ((startIndex + NumericButtonCount) > PageCount)
                            ? PageCount
                            : (startIndex + NumericButtonCount);
    
        if (ShowMoreButtons && startIndex > 0)
        {
            startMoreButtons = $"
  • {string.Format(PageURL, startIndex)}\">...
  • "
    ; } if (ShowPageIndex) { for (int i = startIndex + 1; i <= endIndex; i++) { if (i == pageIndex) { PageControl = PageControl + string.Format("
  • {0}
  • "
    , i.ToString()); } else { PageControl = PageControl + string.Format("
  • {1}
  • "
    , string.Format(pageURL, i.ToString()), i.ToString()); } } PageControl = startMoreButtons + PageControl; } if (ShowMoreButtons && (totalPageCount > ShowNum) && ((endIndex < PageCount) || (PageCount > endIndex + 1))) { endMoreButtons = $"
  • {string.Format(PageURL, (endIndex + 1))}\">...
  • "
    ; } PageControl = PageControl + endMoreButtons; if (pageIndex != 1) { PageControl = string.Format("
  • {1}
  • "
    , string.Format(pageURL, (pageIndex - 1).ToString()), PrevPageText) + PageControl; } else { PageControl = string.Format("
  • {0}
  • "
    , PrevPageText) + PageControl; } if (pageIndex != totalPageCount) { PageControl = PageControl + string.Format("
  • {1}
  • "
    , string.Format(pageURL, (pageIndex + 1).ToString()), NextPageText); } else { PageControl = PageControl + string.Format("
  • {0}
  • "
    , NextPageText); } string FirstPage = string.Format("
  • {1}
  • ", string.Format(pageURL, "1"), FirstPageText); string LastPage = string.Format("
  • {1}
  • "
    , string.Format(pageURL, totalPageCount.ToString()), LastPageText); if (ShowFirstLast) { PageControl = FirstPage + PageControl; PageControl = PageControl + LastPage; } //完成容器包装 PageControl = string.Format("", string.IsNullOrEmpty(Class) ? "" : " class=\"" + Class + "\"") + PageControl + "
"; pageURL = PageControl; return pageURL; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

代码主要是根据分页数、当前页以及需要显示几页,来计算是否显示更多页的按钮,代码还可以设置是否显示第一页和最后一页,可以设置是否显示更多页按钮。还可以设置上一页,下一页的的文字以及首页和末页的文字。

实际应用

[Route("/news/list?page={pageIndex}")]
public async Task<IActionResult> List(int pageIndex)
{

    PageHelper pager = new PageHelper();
    pager.PageURL = "/news/list?page={0}";
    pager.PageSize = 10;
    pager.CurrentPageIndex = pageIndex;
    pager.Class = "pagination";
    pager.ShowNum = 5;
    pager.ShowFirstLast = true;
    pager.NextPageText = ">";//默认是下一页
    pager.PrevPageText = "<";//默认是上一页
    pager.FirstPageText = "<<";//默认是首页
    pager.LastPageText = ">>";//默认是末页
    pager.ShowFirstLast = false;
    var list = await _article_provider.GetPageListAsync();
    pager.RecordCount = list.count;
    return View(Tuple.Create(list.data, pager));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

使用Tuple将数据与分页传递到前端,前端需要输出一下分页:

 @model Tuple<List<news>,PageHelper>
 @Html.Raw(Model.Item2.pager())
  • 1
  • 2

自己完的一个CSS

自己写了一个前端的css样式。分享给大家作为参考:

.pagination{text-align:center; padding:20px 40px;}
.pagination li{display:inline-block; margin:0 5px;}
.pagination li span,.pagination li a{display:block;background:#fff; height:34px; line-height:34px; padding:0 12px; border:1px #edecec solid;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;-o-user-select: none;user-select: none;}
.pagination li.active span,.pagination li.active a{background:#f7a511; color:#fff; border:1px #f7a511 solid;}
.pagination li.disabled span,.pagination li.disabled a{color:#edecec;}
.pagination li a:hover{background:#f7a511; color:#fff; border:1px #f7a511 solid;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

因为在应用中,给定的css是pagination,所以css定义pagination样式就可以啦!

总结

新建一个类,给类起个名字,把代码贴进去就可以使用了!

点击下方公众号卡片,关注我!一起学习,一起进步!

  • 相关阅读:
    以太坊代币标准ERC20、ERC165、ERC721
    CSS中如何实现文字描边效果(Text Stroke)?
    typeScript核心篇——通过TS写es6类型编程:枚举,类型推论,类型兼容性,高级类型
    Git小书系列笔记
    测试用例设计方法六脉神剑——第二剑:招式组合,因果判定出世
    ARCGIS进行视域分析及地形图制作
    你好GPT-4o——对GPT-4o发布的思考与看法
    Java岗位必备技能SpringBoot的面试题集锦
    Redis对字符串的操作汇总
    js 不同域iframe 与父页面消息通信
  • 原文地址:https://blog.csdn.net/sd2208464/article/details/128182479