首页 > 代码库 > 分页方法总结
分页方法总结
在同一个命名空间下:
1.定义接口
public interface IPagedList<T> : IList<T> { int PageIndex { get; } int PageSize { get; } int TotalCount { get; } int TotalPages { get; } bool HasPreviousPage { get; } bool HasNextPage { get; } }
注意:IList<T> 继承了ICollection<T>, IEnumerable<T>, Ienumerable
2.实现接口:
[Serializable]//讲当前类的实例序列化,便于网络中传输和保存 public class PagedList<T> : List<T>, IPagedList<T> { /// <summary> /// Ctor /// </summary> /// <param name="source">source</param> /// <param name="pageIndex">Page index</param> /// <param name="pageSize">Page size</param> public PagedList(IQueryable<T> source, int pageIndex, int pageSize) { int total = source.Count(); this.TotalCount = total; this.TotalPages = total / pageSize; if (total % pageSize > 0) TotalPages++; this.PageSize = pageSize; this.PageIndex = pageIndex; this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList()); } /// <summary> /// Ctor /// </summary> /// <param name="source">source</param> /// <param name="pageIndex">Page index</param> /// <param name="pageSize">Page size</param> public PagedList(IList<T> source, int pageIndex, int pageSize) { TotalCount = source.Count(); TotalPages = TotalCount / pageSize; if (TotalCount % pageSize > 0) TotalPages++; this.PageSize = pageSize; this.PageIndex = pageIndex; this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList()); } /// <summary> /// Ctor /// </summary> /// <param name="source">source</param> /// <param name="pageIndex">Page index</param> /// <param name="pageSize">Page size</param> /// <param name="totalCount">Total count</param> public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount) { TotalCount = totalCount; TotalPages = TotalCount / pageSize; if (TotalCount % pageSize > 0) TotalPages++; this.PageSize = pageSize; this.PageIndex = pageIndex; this.AddRange(source); } public int PageIndex { get; private set; } public int PageSize { get; private set; } public int TotalCount { get; private set; } public int TotalPages { get; private set; } public bool HasPreviousPage { get { return (PageIndex > 0); } } public bool HasNextPage { get { return (PageIndex + 1 < TotalPages); } } }
3.功能的实现(控制器中)
从数据源(数据库)查询数据 为Hus
var aHus = new PagedList<Entity>(Hus, command.Page - 1, command.PageSize);// Entity:需要分页的实体, Hus:查询后的总数据 var gridModel = new DataSourceResult { Data = aHus.Select(x => { // var m = x.ToModel(); var m=new EntityModel(); // EntityModel界面model 赋值。。。。 return m; }), Total = aHus.TotalCount }; return Json(gridModel);
可简写为:
var gridModel = new DataSourceResult { Data = new PagedList<Entity>(Hus, command.Page - 1, command.PageSize).Select(x => { //var m = x.ToModel(); var m=new EntityModel(); // EntityModel界面model 赋值。。。。 return m; }), Total = aHus.TotalCount }; return Json(gridModel);
注意:DataSourceResult类为 (功能为装数据)
public class DataSourceResult { public IEnumerable Data { get; set; } public int Total { get; set; } }
4.前端代码(省去)
分页方法总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。