自己寫的一個Pager分頁組件,WebForm,Mvc都適用


           我一說寫這個功能的時候,好多人估計有疑問。分頁功能網上多的是,搜一個不就行了,你這樣不是浪費時間么。你說這句話的時候,我是比較信的,首先自己寫一些東西是很耗時,有這些時間又能多打幾盤LOL了。但是我覺得有些東西需要你自己寫了之后思路才清晰,一直認為知道一些原理才是最重要的,並且能讓你更深入的去了解一門語言。就像好多人說“我精通JQuery框架”,那我想問你“有沒有看過Jquery源碼,知不知道它的實現原理和機制?”。熟練和精通不是同一個層次,首先熟練是你會使用一些框架的語法和用法,精通是指你知道其中的原理和機制,並且能夠模仿並延伸一些知識。我說這話的意思是讓大家帶着一股鑽研精神去學習一門語言,當做一種興趣和愛好,而不僅僅是把它當做一個飯碗,混混日子那么簡單。

         說到這個功能,我不得不提一下一個叫楊濤的大叔,一個善於與別人分享資源的人,你可以稱他為“分頁達人”,寫了各種分頁的擴展,他的個人主頁地址http://www.webdiyer.com/,大家沒事可以看看,想必也有好多人用過他寫的分頁控件。

         下面來說說正題,這個功能其實是我兩年前寫的,今天無意間看到,所以只是想和大家分享一下,另外也算是做個筆記,說下我的思路。

          分頁控件其實就是根據鏈接在頁面間傳遞參數,因為我看到MVC中你可以看到這樣傳遞參數的new {para=val}這種方式傳遞參數,於是我想到用可以模仿這種傳遞參數的方式,那就用dynamic來作為參數對象傳遞。

          下面是附上我寫的具體的實現的代碼

        數據處理代碼:

        1.定義IPagedList接口

       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastruction.Pager
{
    public interface IPagedList
    {
        int pageIndex { get; set; }
        int pageSize { get; set; }
        int totalItemCount { get; set; }
        int totalPageCount { get; }
    }
}

        2.實現IPagedList接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Infrastruction.Pager
{
    public class PagedList<T> : List<T>, IPagedList
    {
        public int pageIndex
        {
            get;
            set;
        }

        public int pageSize
        {
            get;
            set;
        }

        public int totalItemCount
        {
            get;
            set;
        }

        public int totalPageCount
        {
            get
            {
                return totalItemCount % pageSize == 0 ? (totalItemCount / pageSize) : (totalItemCount / pageSize + 1);
            }
        }

        public PagedList(IEnumerable<T> sources, int pageIndex, int pageSize)
        {
            if (sources != null && sources.Any())
            {
                this.AddRange(sources.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList());
            }
            this.pageIndex = pageIndex;
            this.pageSize = pageSize;
            this.totalItemCount = sources.Count();
        }
    }
}

        分頁標簽處理代碼:

        3.PagerHelper

        

using Infrastruction.Pager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;

namespace System.Web.UI
{
    public static class PagerHelper
    {

        public static string Pager(string url, IPagedList pagedList)
        {
            StringBuilder builder = new StringBuilder();
            if (pagedList != null)
            {
                builder.Append("<script type='text/javascript'>");
                builder.Append("window.onload = function () {");
                builder.Append(" var elements = document.getElementById('pager').childNodes;");
                builder.Append(" for (var i = 0; i < elements.length; i++) {");
                builder.Append("var txt = elements[i].innerText || elements[i].textContent;");
                builder.Append(" if (elements[i].nodeType =='1' && txt == '" + pagedList.pageIndex + "') {");
                builder.Append("elements[i].style.textDecoration = 'underline';break; } } }");
                builder.Append("</script>");
                builder.Append("<div id='pager'>");

                builder.Append("<span class='p'>");
                builder.AppendFormat("共 {0} 條數據   頁次:{1}/{2}",  pagedList.totalItemCount, pagedList.pageIndex, pagedList.totalPageCount, "上一頁");
                builder.Append("</span>");
                builder.Append("&nbsp;");
                builder.Append("&nbsp;");
                builder.Append("&nbsp;");
                builder.Append("&nbsp;");
                if (pagedList.pageIndex > 1 && pagedList.pageIndex <= pagedList.totalPageCount)
                {
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, 1, "首頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, pagedList.pageIndex - 1, "上一頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                }
                if (pagedList.totalPageCount > 1 && pagedList.totalPageCount <= 10)
                {
                    for (int i = 1; i <= pagedList.totalPageCount; i++)
                    {
                        builder.Append("<span class='p'>");
                        builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, i, i);
                        builder.Append("</span>");
                        builder.Append("&nbsp;");
                    }
                }
                else if (pagedList.totalPageCount > 10)
                {
                    if (pagedList.pageIndex < 11)
                    {
                        for (int i = 1; i <= 10; i++)
                        {
                            builder.Append("<span class='p'>");
                            builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, i, i);
                            builder.Append("</span>");
                            builder.Append("&nbsp;");
                        }
                        builder.Append("<span class='p'>");
                        builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, 11, "...");
                        builder.Append("</span>");
                        builder.Append("&nbsp;");
                    }
                    else
                    {
                        builder.Append("<span class='p'>");
                        builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, (pagedList.pageIndex - 6), "...");
                        builder.Append("</span>");
                        builder.Append("&nbsp;");
                        if (pagedList.pageIndex >= 11 && pagedList.totalPageCount <= pagedList.pageIndex + 5)
                        {
                            for (int i = pagedList.pageIndex - 5; i <= pagedList.totalPageCount; i++)
                            {
                                builder.Append("<span class='p'>");
                                builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, i, i);
                                builder.Append("</span>");
                                builder.Append("&nbsp;");
                            }
                        }
                        else
                        {
                            for (int i = pagedList.pageIndex - 5; i <= pagedList.pageIndex + 5; i++)
                            {
                                builder.Append("<span class='p'>");
                                builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, i, i);
                                builder.Append("</span>");
                                builder.Append("&nbsp;");
                            }
                            builder.Append("<span class='p'>");
                            builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, (pagedList.pageIndex + 6), "...");
                            builder.Append("</span>");
                            builder.Append("&nbsp;");
                        }
                    }

                }
                if (pagedList.pageIndex >= 1 && pagedList.pageIndex < pagedList.totalPageCount)
                {
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, pagedList.pageIndex + 1, "下一頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?pageIndex={1}'>{2}</a>", url, pagedList.totalPageCount, "尾頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                }
                builder.Append("</div>");
            }
            return builder.ToString();
        }


        public static string Pager(string url, IPagedList pagedList, dynamic objAttr)
        {
            StringBuilder builder = new StringBuilder();
            if (pagedList != null)
            {
                builder.Append("<script type='text/javascript'>");
                builder.Append("window.onload = function () {");
                builder.Append(" var elements = document.getElementById('pager').childNodes;");
                builder.Append(" for (var i = 0; i < elements.length; i++) {");
                builder.Append("var txt = elements[i].innerText || elements[i].textContent;");
                builder.Append(" if (elements[i].nodeType =='1' && txt == '" + pagedList.pageIndex + "') {");
                builder.Append("elements[i].style.textDecoration = 'underline';break; } } }");
                builder.Append("</script>");
                string paras = "";
                PropertyInfo[] infos = objAttr.GetType().GetProperties();
                if (infos != null && infos.Any())
                {
                    foreach (var item in infos)
                    {
                        paras += string.Format("{0}={1}", item.Name, item.GetValue(objAttr, null));
                        paras += "&";
                    }
                }
                paras = paras + "pageIndex=";
                builder.Append("<div id='pager'>");

                builder.Append("<span class='p'>");
                builder.AppendFormat("共 {0} 條數據   頁次:{1}/{2}", pagedList.totalItemCount, pagedList.pageIndex, pagedList.totalPageCount, "上一頁");
                builder.Append("</span>");
                builder.Append("&nbsp;");
                builder.Append("&nbsp;");
                builder.Append("&nbsp;");
                builder.Append("&nbsp;");


                if (pagedList.pageIndex > 1 && pagedList.pageIndex <= pagedList.totalPageCount)
                {
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + 1, "首頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + (pagedList.pageIndex - 1), "上一頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                }
                if (pagedList.totalPageCount > 1 && pagedList.totalPageCount <= 10)
                {
                    for (int i = 1; i <= pagedList.totalPageCount; i++)
                    {
                        builder.Append("<span class='p'>");
                        builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + i, i);
                        builder.Append("</span>");
                        builder.Append("&nbsp;");
                    }
                }
                else if (pagedList.totalPageCount > 10)
                {
                    if (pagedList.pageIndex < 11)
                    {
                        for (int i = 1; i <= 10; i++)
                        {
                            builder.Append("<span class='p'>");
                            builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + i, i);
                            builder.Append("</span>");
                            builder.Append("&nbsp;");
                        }
                        builder.Append("<span class='p'>");
                        builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + 11, "...");
                        builder.Append("</span>");
                        builder.Append("&nbsp;");
                    }
                    else
                    {
                        builder.Append("<span class='p'>");
                        builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + (pagedList.pageIndex - 6), "...");
                        builder.Append("</span>");
                        builder.Append("&nbsp;");
                        if (pagedList.pageIndex >= 11 && pagedList.totalPageCount <= pagedList.pageIndex + 5)
                        {
                            for (int i = pagedList.pageIndex - 5; i <= pagedList.totalPageCount; i++)
                            {
                                builder.Append("<span class='p'>");
                                builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + i, i);
                                builder.Append("</span>");
                                builder.Append("&nbsp;");
                            }
                        }
                        else
                        {
                            for (int i = pagedList.pageIndex - 5; i <= pagedList.pageIndex + 5; i++)
                            {
                                builder.Append("<span class='p'>");
                                builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + i, i);
                                builder.Append("</span>");
                                builder.Append("&nbsp;");
                            }
                            builder.Append("<span class='p'>");
                            builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + (pagedList.pageIndex + 6), "...");
                            builder.Append("</span>");
                            builder.Append("&nbsp;");
                        }

                    }

                }
                if (pagedList.pageIndex >= 1 && pagedList.pageIndex < pagedList.totalPageCount)
                {
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + (pagedList.pageIndex + 1), "下一頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                    builder.Append("<span class='p'>");
                    builder.AppendFormat("<a  href='{0}?{1}'>{2}</a>", url, paras + pagedList.totalPageCount, "尾頁");
                    builder.Append("</span>");
                    builder.Append("&nbsp;");
                }
                builder.Append("</div>");
            }
            return builder.ToString();
        }



    }
}
View Code

 

        4.  PagerLinqExtension( 基於linq的擴展)

        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace Infrastruction.Pager
{
    public static class PagerLinqExtension
    {

        public static PagedList<T> ToPagedList<T>(this  IQueryable<T> source, int pageIndex, int pageSize)
        {
            return new PagedList<T>(source, pageIndex, pageSize);
        }

    }
}

調用方法  

   1.Webform調用:   <%=PagerHelper.Pager("Products.aspx", pageList, new { cid=Cid})%>   或者用literal在后台綁定也行 

   2.Mvc調用:

   需要擴展一下方法

   

namespace System.Web.Mvc.Html
{
    public static class HtmlExtension
    {
        public static IHtmlString Pager(this  HtmlHelper helper, string url, IPagedList pagedList)
        {
            return helper.Raw(PagerHelper.Pager(url, pagedList));
        }
        public static IHtmlString Pager(this  HtmlHelper helper, string url, IPagedList pagedList, dynamic objAttr)
        {
            return helper.Raw(PagerHelper.Pager(url, pagedList, objAttr));
        }
   }
}

 

然后頁面調用@Html.Pager("Products.aspx", pageList, new { cid=Cid,......})

         全部的代碼都在上面,大家想看的可以看看,有不同想法的也可以和我交流下,左上角有qq。


免責聲明!

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



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