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.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 Code1.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 Code1.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 Code1.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));elseAddRange(source.Skip(PageIndex * PageSize).Take(PageSize));}#region IPagedList Memberspublic 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 Code1.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 extensionspublic 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 extensionspublic 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 extensionspublic 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";elsecss = "sort-asc";}elsecss = "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";elsecss = "sort-asc";}elsecss = "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";elsecss = "sort-asc";}elsecss = "sort-off";var htmlAttributes = new { @class = css };return htmlHelper.ActionLink(name, actionName, routeValues, htmlAttributes);}#endregion#region IQueryable<T> extensionspublic static IPagedList<T> ToPagedList<T>(this IQueryable<T> source, IPagingOption option){return new PagedList<T>(source, option);}#endregion#region IEnumerable<T> extensionspublic static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, IPagingOption option){return new PagedList<T>(source, option);}#endregion} } View Code1.B.7,
| 1.C,下載地址返回頂部 |
?
| 作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 |
總結
以上是生活随笔為你收集整理的cs-Panination的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序引入ColorUI
- 下一篇: Linux 系统应用编程——网络编程(T