.NetCore 分页控件实现原理处理以及条件分页处理
说明
自定义一个类继承TagHelper,注意自定义类的 必须以TagHelper结尾,这个有点类是属性 Attribute的写法
protected TagHelper(); // // 摘要: // When a set of Microsoft.AspNetCore.Razor.TagHelpers.ITagHelpers are executed, // their Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Init(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext)'s // are first invoked in the specified Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Order; // then their Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext,Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput)'s // are invoked in the specified Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Order. // Lower values are executed first. // // 备注: // Default order is 0. public virtual int Order { get; } // // 摘要: // Initializes the Microsoft.AspNetCore.Razor.TagHelpers.ITagHelper with the given // context. Additions to Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext.Items // should be done within this method to ensure they're added prior to executing // the children. // // 参数: // context: // Contains information associated with the current HTML tag. // // 备注: // When more than one Microsoft.AspNetCore.Razor.TagHelpers.ITagHelper runs on the // same element, TagHelperOutput.GetChildContentAsync may be invoked prior to Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext,Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput). public virtual void Init(TagHelperContext context); // // 摘要: // Synchronously executes the Microsoft.AspNetCore.Razor.TagHelpers.TagHelper with // the given context and output. // // 参数: // context: // Contains information associated with the current HTML tag. // // output: // A stateful HTML element used to generate an HTML tag. public virtual void Process(TagHelperContext context, TagHelperOutput output); // // 摘要: // Asynchronously executes the Microsoft.AspNetCore.Razor.TagHelpers.TagHelper with // the given context and output. // // 参数: // context: // Contains information associated with the current HTML tag. // // output: // A stateful HTML element used to generate an HTML tag. // // 返回结果: // A System.Threading.Tasks.Task that on completion updates the output. // // 备注: // By default this calls into Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Process(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext,Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput). public virtual Task ProcessAsync(TagHelperContext context, TagHelperOutput output);
重写一个 ProcessAsync 这里我以异步为例子
首先说明下分页需要的重要参数 定义一个分页参数类
public class PagerOptions { /// <summary> /// 每页数据条数 /// </summary> public int PageSize { get; set; } /// <summary> /// 当前页码 /// </summary> public int CurrentPageIndex { get; set; } /// <summary> /// 数据总条数 /// </summary> public int ItemTotal { get; set; } /// <summary> /// 总页数 /// </summary> public int PageTotal { get { return ItemTotal % PageSize > 0 ? ItemTotal / PageSize + 1 : ItemTotal / PageSize; } } /// <summary> /// 显示的页面个数(只显示5个页码) /// </summary> public int EveryCount { get; set; } /// <summary> /// 允许选择页码 /// </summary> public bool IsSelectPageSize { get; set; } /// <summary> /// 每页数据条数范围 /// </summary> public int[] SelectPageSize { get; set; } /// <summary> /// 是否显示转到页码 /// </summary> public bool IsGoPage { get; set; } /// <summary> /// 分页访问地址 /// </summary> public string PageUri { get; set; } }
public class PagerTagHelper : TagHelper { public PagerOptions PagerOption { get; set; } public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { output.TagName = "div"; if (PagerOption.PageSize <= 0) { PagerOption.PageSize = 10; } if (PagerOption.CurrentPageIndex <= 0) { PagerOption.CurrentPageIndex = 1; } if (PagerOption.CurrentPageIndex > PagerOption.PageTotal) { PagerOption.CurrentPageIndex = PagerOption.PageTotal; } if (PagerOption.PageTotal <= 0) { return Task.CompletedTask; } string ax = PagerOption.PageUri; //接下来就是拼写html样式而已 return base.ProcessAsync(context, output); } }
样式具体没有实现值说下原理,添加以上类注意对命名空间的引用,不然是无法编写服务器标签的
找到_ViewImports.cshtml文件 中添加
@addTagHelper "ExpressUser.PagerTagHelper,ExpressUser" @addTagHelper "ExpressUser.PagerOptions,ExpressUser"
然后在页面上编写pager服务器标签 这是是pager 对应的是类 PagerTagHelper 参数类型 PagerOptions 在 PagerTagHelper 中的变量是 PagerOption 所以这里对应 pager-option
其实就这样简单,当然在代码中可以这样使用 ,如果点击分页按钮要保持查询条件分页,这里就需要获取条件了,这就你是什么方式的请求的了
这里分页我以Get为例子
处理下这个对象
public abstract IQueryCollection Query { get; set; }
或者
public abstract IFormCollection Form { get; set; }
var list = Request.Query.ToList(); string querystring = string.Empty; foreach (var item in list) { querystring += "&" + item.Key + "=" + item.Value; } ViewBag.PagerOption = new PagerOptions() { ItemTotal = 23, PageUri = Request.Path + (string.IsNullOrEmpty(querystring) ? "" : "?" + querystring.Substring(1)) };
赋值PagerUri 就行了
下面在回到PagerTagHelper中访问看下
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!
本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/liyouming欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接。