.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; }
    }
PagerOptions
 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);
        }

    }
PagerTagHelper

樣式具體沒有實現值說下原理,添加以上類注意對命名空間的引用,不然是無法編寫服務器標簽的

找到_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中訪問看下

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM