分页方法总结

Posted 与你在巅峰相会

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分页方法总结相关的知识,希望对你有一定的参考价值。

在同一个命名空间下:

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.前端代码(省去)

以上是关于分页方法总结的主要内容,如果未能解决你的问题,请参考以下文章

FragmentStatePagerAdapter视图分页器片段在重新创建活动后未显示

BootStrap有用代码片段(持续总结)

BootStrap实用代码片段(持续总结)

modx - 当我在同上片段中使用“&documents =”参数时,分页不起作用

springmvc 分页查询的简单实现

片段被视图分页器布局切割