首页 > 代码库 > cs-Panination

cs-Panination

ylbtech-Unitity: cs-Panination

Pager.cs 

IPagingOption.cs IPagedList.cs

PagingOption.cs PagedList.cs

PagingExtensions.cs

1.A,效果图返回顶部
 
1.B,源代码返回顶部
1.B.1,Pager.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web;using System.Web.Mvc;using System.Web.Mvc.Ajax;using System.Web.Routing;using Healthcare.Framework.Web.URL;namespace Healthcare.Framework.Web.Mvc.Pagination{    public class Pager    {         private ViewContext viewContext;        private int currentPage;        private int pageCount;        private RouteValueDictionary pageLinkValueDictionary;        private readonly int pageSize;        private readonly int totalItemCount;        private readonly RouteValueDictionary linkWithoutPageValuesDictionary;        private readonly AjaxOptions ajaxOptions;        private readonly string sortBy;        private readonly bool? sortDescending;        public Pager(ViewContext viewContext, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)        {            this.viewContext = viewContext;            this.pageSize = pageSize;            this.currentPage = currentPage;            this.totalItemCount = totalItemCount;            this.linkWithoutPageValuesDictionary = valuesDictionary;            this.ajaxOptions = ajaxOptions;            this.ensureCollectionPageLinkValue();        }        public Pager(ViewContext viewContext, int pageSize, int currentPage, int totalItemCount, string sortBy, bool? sortDescending, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)        {            this.viewContext = viewContext;            this.pageSize = pageSize;            this.currentPage = currentPage;            this.totalItemCount = totalItemCount;            this.sortBy = sortBy;            this.sortDescending = sortDescending;            this.linkWithoutPageValuesDictionary = valuesDictionary;            this.ajaxOptions = ajaxOptions;            this.ensureCollectionPageLinkValue();        }        public HtmlString RenderHtml()        {            pageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize);            if (pageCount == 0) pageCount = 1;            const int nrOfPagesToDisplay = 4;            if (pageCount < currentPage)                currentPage = pageCount;            var sb = new StringBuilder();            // Previous            sb.Append("<ul class=\"pagination pull-right\">");            sb.Append(currentPage > 1 ? GeneratePageLink("«", 1, "first") : "<li class=\"first disabled\"><a>«</a></li>");            sb.Append(currentPage > 1 ? GeneratePageLink("", currentPage - 1, "previous") : "<li class=\"previous disabled\"><a>‹</a></li>");            var start = 1;            var end = pageCount;            if (pageCount > nrOfPagesToDisplay)            {                var middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1;                var below = (currentPage - middle);                var above = (currentPage + middle);                if (below < 4)                {                    above = nrOfPagesToDisplay;                    below = 1;                }                else if (above > (pageCount - 4))                {                    above = pageCount;                    below = (pageCount - nrOfPagesToDisplay);                }                start = below;                end = above;            }            if (start > 3)            {                sb.Append(GeneratePageLink("1", 1));                sb.Append(GeneratePageLink("2", 2));                sb.Append("<li class=\"disabled\"><a>...</a></li>");            }            for (var i = start; i <= end; i++)            {                if (i == currentPage || (currentPage <= 0 && i == 0))                {                    sb.AppendFormat("<li class=\"active\"><a>{0}</a></li>", i);                }                else                {                    sb.Append(GeneratePageLink(i.ToString(), i));                }            }            //if (end < (pageCount - 5))            //{            //    sb.Append("<li class=\"disabled\"><a>...</a></li>");            //    sb.Append(GeneratePageLink((pageCount - 3).ToString(), pageCount - 3));            //    sb.Append(GeneratePageLink((pageCount - 2).ToString(), pageCount - 2));            //    sb.Append(GeneratePageLink((pageCount - 1).ToString(), pageCount - 1));            //    sb.Append(GeneratePageLink(pageCount.ToString(), pageCount));            //}            if (end  < pageCount)//后面还有其它页码            {                int tempmin = pageCount - 3;                sb.Append("<li class=\"disabled\"><a>...</a></li>");                for (int k = tempmin; k <= pageCount; k++)                {                    sb.Append(GeneratePageLink(k.ToString(), k));                }                //sb.Append(GeneratePageLink(pageCount.ToString(), pageCount));            }            // Next            sb.Append(currentPage < pageCount ? GeneratePageLink("", (currentPage + 1), "next") : "<li class=\"next disabled\"><a>›</a></li>");            sb.Append(currentPage < pageCount ? GeneratePageLink("»", pageCount, "last") : "<li class=\"last disabled\"><a>»</a></li>");            sb.Append("</ul>");            return new HtmlString(sb.ToString());        }        private string GeneratePageLink(string linkText, int pageNumber, string classText = "")        {            if (pageLinkValueDictionary.ContainsKey("Page"))            {                pageLinkValueDictionary.Remove("Page");            }            pageLinkValueDictionary.Add("Page", pageNumber);            // ‘Render‘ virtual path.            var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLinkValueDictionary);            if (virtualPathForArea == null)                return null;            var stringBuilder = new StringBuilder("<li class=\"" + classText + "\"><a");            if (ajaxOptions != null)                foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes())                    stringBuilder.AppendFormat(" {0}=\"{1}\"", ajaxOption.Key, ajaxOption.Value);            stringBuilder.AppendFormat(" href=http://www.mamicode.com/"{0}\">{1}</a></li>", virtualPathForArea.VirtualPath, linkText);            return stringBuilder.ToString();        }        public HtmlString RenderGotoBaseUrl()        {            if (pageLinkValueDictionary.ContainsKey("Page"))            {                pageLinkValueDictionary.Remove("Page");            }            var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLinkValueDictionary);            return new HtmlString(virtualPathForArea.VirtualPath);        }        public HtmlString RenderSwitchPageSizeBaseUrl()        {            if (pageLinkValueDictionary.ContainsKey("PageSize"))            {                pageLinkValueDictionary.Remove("PageSize");            }            if (pageLinkValueDictionary.ContainsKey("Page"))            {                pageLinkValueDictionary.Remove("Page");            }            var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLinkValueDictionary);            return new HtmlString(virtualPathForArea.VirtualPath);        }        private void ensureCollectionPageLinkValue()        {            pageLinkValueDictionary = new RouteValueDictionary(linkWithoutPageValuesDictionary) { { "SortBy", sortBy }, { "SortDescending", sortDescending } };            //{ { "Page", pageNumber }, { "PageSize", pageSize }, };            Dictionary<string, string> dict = new Dictionary<string, string>();            QueryStringHelper.ExtractQueryStringValuesFromUri(viewContext.HttpContext.Request.Url, ref dict);            dict.Keys.ToList().ForEach(key =>            {                if (!pageLinkValueDictionary.ContainsKey(key))                {                    pageLinkValueDictionary.Add(key, dict[key]);                }            });            // To be sure we get the right route, ensure the controller and action are specified.            var routeDataValues = viewContext.RequestContext.RouteData.Values;            routeDataValues.Keys.ToList().ForEach(key =>            {                if (!pageLinkValueDictionary.ContainsKey(key))                {                    pageLinkValueDictionary.Add(key, routeDataValues[key]);                }            });                    }    }}
View Code

1.B.2,IPagingOption.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.Routing;namespace Healthcare.Framework.Web.Mvc.Pagination{    public interface IPagingOption    {         int Page { get; set; }        int PageSize { get; set; }        int? TotalCount { get; set; }        string SortBy { get; set; }        bool? SortDescending { get; set; }        string OrderByExpression { get; }        RouteValueDictionary RouteValues { get; set; }    }}
View Code

1.B.3,IPagedList.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.Routing;namespace Healthcare.Framework.Web.Mvc.Pagination{    public interface IPagedList<T> : IList<T>    {         int PageCount { get; }        int TotalItemCount { get; }        int PageIndex { get; }        int PageNumber { get; }        int PageSize { get; }        bool HasPreviousPage { get; }        bool HasNextPage { get; }        bool IsFirstPage { get; }        bool IsLastPage { get; }        string SortBy { get; }        bool? SortDescending { get; }        RouteValueDictionary RouteValues { get; }    }}
View Code

1.B.4,PagingOption.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.Routing;namespace Healthcare.Framework.Web.Mvc.Pagination{    public class PagingOption : IPagingOption    {        public int Page { get; set; }        public int PageSize { get; set; }        public int? TotalCount { get; set; }        public Int32? IsPagination { get; set; }        public string ActionName { get; set; }        public string SortBy { get; set; }        public bool? SortDescending { get; set; }        public bool ShowSummary { get; set; }        public bool ShowGoto { get; set; }        public bool ShowPageSize { get; set; }        public PagingOption()        {            TotalCount = 0;            PageSize = 10;            Page = 1;        }        public RouteValueDictionary RouteValues { get; set; }        public string OrderByExpression        {            get            {                return this.SortDescending.HasValue && this.SortDescending.Value ? this.SortBy + " desc" : this.SortBy + " asc";            }        }        public int PageTotal        {            get            {                if (TotalCount == null)                {                    return (int)Math.Ceiling(0 / (double)PageSize);                }                return (int)Math.Ceiling(TotalCount.Value / (double)PageSize);            }        }    }}
View Code

1.B.5,PagedList.cs

using System;using System.Collections.Generic;using System.Linq;using System.Linq.Dynamic;using System.Text;using System.Threading.Tasks;using System.Web.Routing;namespace Healthcare.Framework.Web.Mvc.Pagination{    public class PagedList<T> : List<T>, IPagedList<T>    {         public PagedList(IEnumerable<T> source, IPagingOption option)            : this(source.AsQueryable(), option)        {        }        public PagedList(IQueryable<T> source, IPagingOption option)        {            if (option == null)                throw new ArgumentOutOfRangeException("PagingOption", "Value can not be null.");            if (option.Page < 0)                throw new ArgumentOutOfRangeException("index", "Value must be greater than 0.");            if (option.PageSize < 1)                throw new ArgumentOutOfRangeException("pageSize", "Value must be greater than 1.");            if (source == null)                source = new List<T>().AsQueryable();            var realTotalCount = source.Count();            PageSize = option.PageSize;            PageIndex = option.Page;            RouteValues = option.RouteValues;            TotalItemCount = option.TotalCount.HasValue ? option.TotalCount.Value : realTotalCount;            PageCount = TotalItemCount > 0 ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) : 0;            SortBy = option.SortBy;            SortDescending = option.SortDescending;            if (!string.IsNullOrEmpty(option.SortBy))                source = source.OrderBy(option.OrderByExpression);            HasPreviousPage = (PageIndex > 0);            HasNextPage = (PageIndex < (PageCount - 1));            IsFirstPage = (PageIndex <= 0);            IsLastPage = (PageIndex >= (PageCount - 1));            if (TotalItemCount <= 0)                return;            var realTotalPages = (int)Math.Ceiling(realTotalCount / (double)PageSize);            if (realTotalCount < TotalItemCount && realTotalPages <= PageIndex)                AddRange(source.Skip((realTotalPages - 1) * PageSize).Take(PageSize));            else                AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));        }        #region IPagedList Members        public int PageCount { get; private set; }        public int TotalItemCount { get; private set; }        public int PageIndex { get; private set; }        public int PageNumber { get { return PageIndex + 1; } }        public int PageSize { get; private set; }        public bool HasPreviousPage { get; private set; }        public bool HasNextPage { get; private set; }        public bool IsFirstPage { get; private set; }        public bool IsLastPage { get; private set; }        public string SortBy { get; private set; }        public bool? SortDescending { get; private set; }        public RouteValueDictionary RouteValues { get; set; }        #endregion    }}
View Code

1.B.6,PagingExtensions.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web;using System.Web.Mvc;using System.Web.Mvc.Ajax;using System.Web.Mvc.Html;using System.Web.Routing;namespace Healthcare.Framework.Web.Mvc.Pagination{    public static class PagingExtensions    {         #region AjaxHelper extensions        public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, AjaxOptions ajaxOptions)        {            return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, null, ajaxOptions);        }        public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string sortBy, bool? sortDescending, string actionName, AjaxOptions ajaxOptions)        {            return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, sortBy, sortDescending, actionName, null, ajaxOptions);        }        public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, AjaxOptions ajaxOptions)        {            return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, actionName, null, ajaxOptions);        }        public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, object values, AjaxOptions ajaxOptions)        {            return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, new RouteValueDictionary(values), ajaxOptions);        }        public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values, AjaxOptions ajaxOptions)        {            return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, null, actionName, new RouteValueDictionary(values), ajaxOptions);        }        public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)        {            return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, valuesDictionary, ajaxOptions);        }        public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string sortBy, bool? sortDescending, string actionName, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)        {            if (valuesDictionary == null)            {                valuesDictionary = new RouteValueDictionary();            }            if (actionName != null)            {                if (valuesDictionary.ContainsKey("action"))                {                    throw new ArgumentException("The valuesDictionary already contains an action.", "actionName");                }                valuesDictionary.Add("action", actionName);            }            var pager = new Pager(ajaxHelper.ViewContext, pageSize, currentPage, totalItemCount, sortBy, sortDescending, valuesDictionary, ajaxOptions);            return pager.RenderHtml();        }        #endregion        #region HtmlHelper extensions        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)        {            return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, null);        }        public static HtmlString PagerT(this HtmlHelper htmlHelper, PagingOption options)        {            if (options == null)                return new HtmlString("");            if (options.RouteValues == null)            {                options.RouteValues = new RouteValueDictionary();            }            var pager = new PagerT(htmlHelper.ViewContext, options.PageSize, options.Page, options.TotalCount.HasValue ? options.TotalCount.Value : 0, options.SortBy, options.SortDescending, options.RouteValues, null);            return pager.RenderHtml();        }        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string sortBy, bool? sortDescending)        {            return Pager(htmlHelper, pageSize, currentPage, totalItemCount, sortBy, sortDescending, null, null);        }        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName)        {            return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, null);        }        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, object values)        {            return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, new RouteValueDictionary(values));        }        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values)        {            return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, null, actionName, new RouteValueDictionary(values));        }        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary)        {            return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, valuesDictionary);        }        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string sortBy, bool? sortDescending, string actionName)        {            return Pager(htmlHelper, pageSize, currentPage, totalItemCount, sortBy, sortDescending, actionName, null);        }        public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string sortBy, bool? sortDescending, string actionName, RouteValueDictionary valuesDictionary)        {            if (valuesDictionary == null)            {                valuesDictionary = new RouteValueDictionary();            }            if (actionName != null)            {                if (valuesDictionary.ContainsKey("action"))                {                    throw new ArgumentException("The valuesDictionary already contains an action.", "actionName");                }                valuesDictionary.Add("action", actionName);            }            var pager = new Pager(htmlHelper.ViewContext, pageSize, currentPage, totalItemCount, sortBy, sortDescending, valuesDictionary, null);            return pager.RenderHtml();        }        public static HtmlString Pager(this HtmlHelper htmlHelper, PagingOption options)        {            if (options == null)                return new HtmlString("");            if (options.RouteValues == null)            {                options.RouteValues = new RouteValueDictionary();            }            var pager = new Pager(htmlHelper.ViewContext, options.PageSize, options.Page, options.TotalCount.HasValue ? options.TotalCount.Value : 0, options.SortBy, options.SortDescending, options.RouteValues, null);            return pager.RenderHtml();        }        public static HtmlString PagerGotoURL(this HtmlHelper htmlHelper, PagingOption options)        {            if (options == null)                return new HtmlString("");            if (options.RouteValues == null)            {                options.RouteValues = new RouteValueDictionary();            }            var pager = new Pager(htmlHelper.ViewContext, options.PageSize, options.Page, options.TotalCount.HasValue ? options.TotalCount.Value : 0, options.SortBy, options.SortDescending, options.RouteValues, null);            return pager.RenderGotoBaseUrl();        }        public static HtmlString PagerGotoURL(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, object values = null)        {            PagingOption options = new PagingOption()            {                Page = 1,                PageSize = pageSize,                TotalCount = totalItemCount,                RouteValues = new RouteValueDictionary(values)            };            return PagerGotoURL(htmlHelper, options);        }        public static HtmlString PagerSwitchPageSizeBaseUrl(this HtmlHelper htmlHelper, PagingOption options)        {            if (options == null)                return new HtmlString("");            if (options.RouteValues == null)            {                options.RouteValues = new RouteValueDictionary();            }            var pager = new Pager(htmlHelper.ViewContext, options.PageSize, options.Page, options.TotalCount.HasValue ? options.TotalCount.Value : 0, options.SortBy, options.SortDescending, options.RouteValues, null);            return pager.RenderSwitchPageSizeBaseUrl();        }        public static HtmlString PagerSwitchPageSizeBaseUrl(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, object values = null)        {            PagingOption options = new PagingOption()            {                Page = 1,                PageSize = pageSize,                TotalCount = totalItemCount,                RouteValues = new RouteValueDictionary(values)            };            return PagerSwitchPageSizeBaseUrl(htmlHelper, options);        }        #endregion        #region ActionLink extensions        public static HtmlString ActionLink<T>(this AjaxHelper ajaxHelper, string name, string actionName, IPagedList<T> model, AjaxOptions ajaxOptions)        {            var sortDescending = model.SortBy != null && model.SortBy.Equals(name) && model.SortDescending.HasValue && model.SortDescending.Value ? false : true;            var routeValues = new RouteValueDictionary();            routeValues.Add("SortBy", name);            routeValues.Add("SortDescending", sortDescending);            if (model.RouteValues != null)            {                foreach (var r in model.RouteValues)                {                    routeValues.Add(r.Key, r.Value);                }            }            IDictionary<string, object> htmlAttributes = new Dictionary<string, object>();            var css = "";            if (!string.IsNullOrEmpty(model.SortBy) && model.SortBy.Equals(name))            {                if (model.SortDescending.HasValue && model.SortDescending.Value)                    css = "sort-desc";                else                    css = "sort-asc";            }            else                css = "sort-off";            htmlAttributes.Add("class", css);            return ajaxHelper.ActionLink(name, actionName, routeValues, ajaxOptions, htmlAttributes);        }        public static HtmlString ActionLink<T>(this AjaxHelper ajaxHelper, string label, string name, string actionName, IPagedList<T> model, AjaxOptions ajaxOptions)        {            var sortDescending = model.SortBy != null && model.SortBy.Equals(name) && model.SortDescending.HasValue && model.SortDescending.Value ? false : true;            var routeValues = new RouteValueDictionary();            routeValues.Add("SortBy", name);            routeValues.Add("SortDescending", sortDescending);            if (model.RouteValues != null)            {                foreach (var r in model.RouteValues)                {                    routeValues.Add(r.Key, r.Value);                }            }            IDictionary<string, object> htmlAttributes = new Dictionary<string, object>();            var css = "";            if (!string.IsNullOrEmpty(model.SortBy) && model.SortBy.Equals(name))            {                if (model.SortDescending.HasValue && model.SortDescending.Value)                    css = "sort-desc";                else                    css = "sort-asc";            }            else                css = "sort-off";            htmlAttributes.Add("class", css);            return ajaxHelper.ActionLink(label, actionName, routeValues, ajaxOptions, htmlAttributes);        }        public static HtmlString ActionLink<T>(this HtmlHelper htmlHelper, string name, string actionName, IPagedList<T> model)        {            var sortDescending = model.SortBy != null && model.SortBy.Equals(name) && model.SortDescending.HasValue && model.SortDescending.Value ? false : true;            var routeValues = new { SortBy = name, SortDescending = sortDescending };            var css = "";            if (!string.IsNullOrEmpty(model.SortBy) && model.SortBy.Equals(name))            {                if (model.SortDescending.HasValue && model.SortDescending.Value)                    css = "sort-desc";                else                    css = "sort-asc";            }            else                css = "sort-off";            var htmlAttributes = new { @class = css };            return htmlHelper.ActionLink(name, actionName, routeValues, htmlAttributes);        }        #endregion        #region IQueryable<T> extensions        public static IPagedList<T> ToPagedList<T>(this IQueryable<T> source, IPagingOption option)        {            return new PagedList<T>(source, option);        }        #endregion        #region IEnumerable<T> extensions        public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, IPagingOption option)        {            return new PagedList<T>(source, option);        }        #endregion    }}
View Code

1.B.7,

1.C,下载地址返回顶部

 

warn作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

cs-Panination