TagHelper的分页查询帮助类
- 帮助类
[HtmlTargetElement("Paging")]
public class PagerTagHelper : TagHelper
{
/// <summary>
/// 数据总数,默认为0条
/// </summary>
public int Total { get; set; } = 0;
/// <summary>
/// 当前页码,默认第一页
/// </summary>
public int PageIndex { get; set; } = 1;
/// <summary>
/// 每页记录数,默认20条
/// </summary>
public int PageSize { get; set; } = 20;
/// <summary>
/// 当前页路由
/// </summary>
public string RouteUrl { get; set; }
/// <summary>
/// 当前页的查询条件
/// </summary>
public string Query { get; set; }
/// <summary>
/// 共多少页
/// </summary>
public int PageTotal
{
get
{
int mainPageCount = Total / PageSize;
if (Total % PageSize > 0)
{
return mainPageCount + 1;
}
return mainPageCount;
}
}
private string SetQueryString()
{
var result = new List<string>();
if (!string.IsNullOrWhiteSpace(Query))
{
if (Query.StartsWith("?"))
{
Query = Query.Remove(0, 1);
}
string[] paramList = Query.Split('&');
foreach (var param in paramList)
{
var paramName = param.Trim().ToLower();
if (!paramName.StartsWith("pageindex=") && !paramName.StartsWith("pagesize="))
{
result.Add(param);
}
}
// 用LINQ遍历
// result = paramList.Where(p => !p.ToLower().StartsWith("pageindex=") && !p.ToLower().StartsWith("pagesize=")).ToList();
}
result.Add("pageIndex={0}");
result.Add("pageSize=" + PageSize.ToString());
return "?" + string.Join('&', result);
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.Attributes.Add("class", "my-pager");
output.PreContent.SetContent($" 共有 {Total} 条记录,每页 {PageSize} 条记录。共 1000 页,当前第 {PageIndex} 页。 ");
if (PageSize <= 0) { PageSize = 20; }
if (PageIndex <= 0) { PageIndex = 1; }
if (Total <= 0) { return; }
//总页数
var totalPage = Total / PageSize + (Total % PageSize > 0 ? 1 : 0);
if (totalPage <= 0) { return; }
Query = SetQueryString();
//构造分页样式
var sbPage = new StringBuilder(string.Empty);
sbPage.Append("<ul class=\"pagination\">");
sbPage.AppendFormat("<li class=\"page-item\"><a class=\"page-link\" href=\"{0}{1}\">首页</a></li>",
RouteUrl,
string.Format(Query, 1)
);
// 计算显示的页码
int start = 1;
int end = totalPage;
bool hasStart = false;
bool hasEnd = false;
if (totalPage > 10)
{
if (PageIndex > 5)
{
start = PageIndex - 4;
hasStart = true;
}
if (start + 9 < totalPage)
{
end = start + 9;
hasEnd = true;
}
else
{
end = totalPage;
start = totalPage - 9;
}
}
if (hasStart)
{
sbPage.AppendFormat("<li class=\"page-item\"><a href=\"{0}{1}\">...</a></li>",
RouteUrl,
string.Format(Query, start - 1)
);
}
for (int i = start; i <= end; i++)
{
sbPage.AppendFormat("<li {1}><a class=\"page-link\" href=\"{2}{3}\">{0}</a></li>",
i,
i == PageIndex ? "class=\"page-item active\"" : "class=\"page-item\"",
RouteUrl,
string.Format(Query, i)
);
}
if (hasEnd)
{
sbPage.AppendFormat("<li class=\"page-item\"><a class=\"page-link\" href=\"{0}{1}\">...</a></li>",
RouteUrl,
string.Format(Query, end + 1)
);
}
sbPage.Append("<li class=\"page-item\">");
sbPage.AppendFormat("<a class=\"page-link\" href=\"{0}{1}\">",
RouteUrl,
string.Format(Query, totalPage));
sbPage.Append("尾页");
sbPage.Append("</a>");
sbPage.Append("</li>");
sbPage.Append("</ul>");
output.Content.SetHtmlContent(sbPage.ToString());
}
}
- 使用方式
public IActionResult Index(string searchString1, string searchString2, string url, int pageIndex = 1, int pageSize = 10)
{
int totalCout = 300;
ConnectionConfig connectionConfig = ConnectionConfigManager.GetConnectionConfigInstance();
using (SqlSugarClient sqlSugarClient = new SqlSugarClient(connectionConfig))
{
var list = sqlSugarClient.Queryable<Commodity>().Take(10).ToList();
PagingData<Commodity> result = new PagingData<Commodity>()
{
DataList = list,
PageIndex = pageIndex,
PageSize = pageSize,
RecordCount = totalCout,
SearchString = searchString1
};
return View(result);
}
}
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/16464051.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。