程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 分頁類,php分頁類

分頁類,php分頁類

編輯:C#入門知識

分頁類,php分頁類


 

/// <summary>
    ///     分頁
    /// </summary>
    /// <typeparam name="T">類型</typeparam>
    public class PagedList<T> : List<T>
    {
        /// <summary>
        ///     緩存分頁構造函數
        /// </summary>
        /// <param name="source">數據源</param>
        /// <param name="pageIndex">當前頁頁碼</param>
        /// <param name="pageSize">每頁顯示記錄數</param>
        public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            if (source == null || !source.Any())
                throw new ArgumentNullException("source");

            int total = source.Count();
            TotalCount = total;
            TotalPages = total/pageSize;

            if (total%pageSize > 0)
                TotalPages++;

            PageSize = pageSize;
            PageIndex = pageIndex;
            AddRange(source.Skip(pageIndex*pageSize).Take(pageSize).ToList());
        }

        /// <summary>
        ///     緩存分頁構造函數
        /// </summary>
        /// <param name="source">數據源</param>
        /// <param name="pageIndex">當前頁頁碼</param>
        /// <param name="pageSize">每頁顯示記錄數</param>
        public PagedList(IList<T> source, int pageIndex, int pageSize)
        {
            if (source == null || !source.Any())
                throw new ArgumentNullException("source");

            TotalCount = source.Count();
            TotalPages = TotalCount/pageSize;

            if (TotalCount%pageSize > 0)
                TotalPages++;

            PageSize = pageSize;
            PageIndex = pageIndex;
            AddRange(source.Skip(pageIndex*pageSize).Take(pageSize).ToList());
        }

        /// <summary>
        ///     非緩存分頁構造函數
        /// </summary>
        /// <param name="source">數據源</param>
        /// <param name="pageIndex">當前頁頁碼</param>
        /// <param name="pageSize">每頁顯示記錄數</param>
        /// <param name="totalCount">總記錄數</param>
        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
        {
            if (source == null || !source.Any())
                throw new ArgumentNullException("source");

            TotalCount = totalCount;
            TotalPages = TotalCount/pageSize;

            if (TotalCount%pageSize > 0)
                TotalPages++;

            PageSize = pageSize;
            PageIndex = pageIndex;
            AddRange(source);
        }

        /// <summary>
        ///     當前頁頁碼
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        ///     每頁顯示記錄數
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        ///     總記錄數
        /// </summary>
        public int TotalCount { get; private set; }

        /// <summary>
        ///     總頁數
        /// </summary>
        public int TotalPages { get; private set; }

        /// <summary>
        ///     有上一頁
        /// </summary>
        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }

        /// <summary>
        ///     有下一頁
        /// </summary>
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPages); }
        }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved