.net后台分页工具类

 public class PagingHelper<T> where T : new()
    {
        private int _PageIndex = 1;
        private int _PageSize = 10;
        private int _Total = 0;
        private int _ShowNum = 5;
        private object _DataSource = null;
        private List<T> _PageData;

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataSource"></param>
        public PagingHelper(object dataSource)
        {
            this._DataSource = dataSource;
            this._Total = this.GetDataSource().Count;
            this._PageData = GetCurrentPage();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSource">数据源</param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页显示记录条数</param>
        public PagingHelper(object dataSource, int index, int size)
        {
            this._DataSource = dataSource;
            this._PageIndex = index;
            this._PageSize = size;
            this._Total = this.GetDataSource().Count;
            this._PageData = GetCurrentPage();
        }

        
        /// <summary>
        /// 直接传当前页数据。必须要把总记录条数一块儿传过来
        /// </summary>
        /// <param name="pageData">当前页数据</param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页记录数</param>
        /// <param name="total">总的记录数</param>
        public PagingHelper(List<T> pageData,int index,int size,int total)
        {
            this._PageIndex = index;
            this._PageSize = size;
            this._Total = total;
            this._PageData = pageData;

        }


        /// <summary>
        /// 每页数据
        /// </summary>
        public List<T> PageData
        {
            get { return _PageData; }
            set { _PageData = value; }
        }

        /// <summary>
        /// 数据源,注意必须是List泛型
        /// </summary>
        public object DataSource
        {
            get { return _DataSource; }
            set { _DataSource = value; }
        }
        /// <summary>
        /// 当前页码,默认1
        /// </summary>
        public int PageIndex
        {
            get { return _PageIndex; }
            set { _PageIndex = value; }
        }

        /// <summary>
        /// 页大小,默认10
        /// </summary>
        public int PageSize
        {
            get { return _PageSize; }
            set { _PageSize = value; }
        }
        /// <summary>
        /// 显示的页数个数,默认5
        /// </summary>
        public int ShowNum
        {
            get { return _ShowNum; }
            set { _ShowNum = value; }
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int Total
        {
            get { return _Total; }
            set { _Total = value; }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int MaxIndex
        {
            get
            {
                return (int)Math.Ceiling(_Total / (_PageSize * 1.0));
            }
        }
        /// <summary>
        /// 是否最后一页
        /// </summary>
        public bool isLast
        {
            get
            {
                return _PageIndex >= MaxIndex ? true : false;
            }
        }
        /// <summary>
        /// 是否首页,即第一页
        /// </summary>
        public bool isFirst
        {
            get
            {
                return _PageIndex == 1 || _PageIndex < 1 ? true : false;
            }
        }
        /// <summary>
        /// 数据源类型转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private List<T> GetDataSource()
        {
            return (List<T>)this._DataSource;
        }
        /// <summary>
        /// 获取当前页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> GetCurrentPage()
        {
            return GetCurrentPage(GetDataSource());
        }
        /// <summary>
        /// 获取当前页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public List<T> GetCurrentPage(List<T> t)
        {
            //ToList<T>()是重点
            List<T> result = t.Skip(this._PageSize * (this._PageIndex - 1)).Take(this._PageSize).ToList<T>();
            return result;
        }
        public List<T> GetCurrentPage(int index)
        {
            _PageIndex = index;
            return GetCurrentPage();
        }
        public List<int> GetIndexList()
        {
            List<int> indexList = new List<int>();
            int endIndex = _PageIndex + ShowNum - 1;
            endIndex = endIndex > MaxIndex ? MaxIndex : endIndex;
            for (int i = 1; i <= endIndex; i++)
            {
                indexList.Add(i);
            }
            return indexList;
        }
        public List<int> GetCurrentIndexList()
        {
            List<int> indexList = new List<int>();
            if (MaxIndex <= ShowNum)
            {
                for (int ii = 1; ii <= MaxIndex; ii++)
                {
                    indexList.Add(ii);
                }
            }
            else
            {
                int start = PageIndex - ShowNum / 2 <= 1 ? 1 : PageIndex - ShowNum / 2;
                start = start + ShowNum - 1 > MaxIndex ? MaxIndex - ShowNum + 1 : start;
                for (int i = start; i <= start + ShowNum - 1; i++)
                {
                    indexList.Add(i);
                }
            }
            return indexList;

        }

    }

    public class PagingInf<T> where T : new()
    {
        /// <summary>
        /// 是否成功获取数据
        /// </summary>
        public bool Result { get; set; }

        /// <summary>
        /// 结果描述
        /// </summary>
        public string ResultDes { get; set; }

        /// <summary>
        /// 是否最后一页
        /// </summary>
        public bool IsLast { get; set; }
       
        /// <summary>
        /// 是否第一页
        /// </summary>
        public bool IsFirst { get; set; }
        
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNum { get; set; }
        
        /// <summary>
        /// 每页显示数据数量
        /// </summary>
        public int PageSize { get; set; }
        
        /// <summary>
        /// 当前页的数据
        /// </summary>
        public List<T> PageData { get; set; }
        
        /// <summary>
        /// 页码列表
        /// </summary>
        public List<int> IndexList { get; set; }
        
        /// <summary>
        /// 记录总数
        /// </summary>
        public int Total { get; set; }

        public PagingInf(object dataSource)
        {
            PagingHelper<T> page = new PagingHelper<T>(dataSource,1,10);
            this.IndexList = page.GetCurrentIndexList();
            this.IsFirst = page.isFirst;
            this.IsLast = page.isLast;
            this.PageData = page.PageData;
            this.PageNum = page.PageIndex;
            this.PageSize = page.PageSize;
            this.Total = page.Total;
            this.Result = true;
        }

        public PagingInf(object dataSource,int index,int size)
        {
            PagingHelper<T> page = new PagingHelper<T>(dataSource, index, size);
            if (page.PageIndex > page.MaxIndex)
            {
                page = new PagingHelper<T>(dataSource, page.MaxIndex, size);
            }
            this.IndexList = page.GetCurrentIndexList();
            this.IsFirst = page.isFirst;
            this.IsLast = page.isLast;
            this.PageData = page.PageData;
            this.PageNum = page.PageIndex;
            this.PageSize = page.PageSize;
            this.Total = page.Total;
            this.Result = true;

        }

        public PagingInf(List<T> pageData, int index, int size, int total)
        {
            PagingHelper<T> page = new PagingHelper<T>(pageData, index, size,total);
            if (page.PageIndex > page.MaxIndex)
            {
                page = new PagingHelper<T>(pageData, page.MaxIndex, size, total);
            }
            this.IndexList = page.GetCurrentIndexList();
            this.IsFirst = page.isFirst;
            this.IsLast = page.isLast;
            this.PageData = page.PageData;
            this.PageNum = page.PageIndex;
            this.PageSize = page.PageSize;
            this.Total = page.Total;
            this.Result = true;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值