一直以来,分页是一个非常常见的需求,以前也用过很多的分页控件,比如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; }
}
基本字段设置内容比较多,大部分都有默认值了。可以不用管,下面介绍分页方法
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;
}
代码主要是根据分页数、当前页以及需要显示几页,来计算是否显示更多页的按钮,代码还可以设置是否显示第一页和最后一页,可以设置是否显示更多页按钮。还可以设置上一页,下一页的的文字以及首页和末页的文字。
[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));
}
使用Tuple将数据与分页传递到前端,前端需要输出一下分页:
@model Tuple<List<news>,PageHelper>
@Html.Raw(Model.Item2.pager())
自己写了一个前端的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;}
因为在应用中,给定的css是pagination
,所以css定义pagination
样式就可以啦!
新建一个类,给类起个名字,把代码贴进去就可以使用了!
点击下方公众号卡片,关注我!一起学习,一起进步!