using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace Commons
 {
     public class PageList<T> : IEnumerable<T>
     {
         private int pageIndex;
         private int pageSize;
         private int totalPage;
         private int totalCount;

         private IList<T> list;

         /// <summary>
         /// 单页数据,总条数,计算出总页数,然后封装成分页对象
         /// </summary>
         /// <param name="list"></param>
         /// <param name="pageIndex"></param>
         /// <param name="pageSize"></param>
         /// <param name="totalCount"></param>
         public PageList(IList<T> list, int pageIndex, int pageSize, int totalCount)
         {
             if (list != null)
             {
                 this.pageIndex = pageIndex;
                 this.pageSize = pageSize;
                 this.totalCount = totalCount;
                 this.totalPage = totalCount / pageSize;
                 ) ;

                 this.list = list;
             }
             else
             {
                 this.list = new List<T>();
             }
         }
         /// <summary>
         /// 全部数据,筛选出当页数据且计算出总条数和总页数,然后封装成分页对象
         /// </summary>
         /// <param name="list"></param>
         /// <param name="pageIndex"></param>
         /// <param name="pageSize"></param>
         public PageList(IList<T> list, int pageIndex, int pageSize)
         {
             if (list != null)
             {
                 this.pageIndex = pageIndex;
                 this.pageSize = pageSize;
                 this.totalCount = list.Count;
                 this.totalPage = list.Count / pageSize;
                 ) ;

                 )).Take(pageSize).ToList();
             }
             else
             {
                 this.list = new List<T>();
             }
         }
         public int PageIndex { get { return this.pageIndex; } }
         public int PageSize { get { return this.pageSize; } }
         public int TotalPage { get { return this.totalPage; } }
         public int TotalCount { get { return this.totalCount; } }

         public IEnumerator<T> GetEnumerator()
         {
             foreach (var item in this.list)
             {
                 yield return item;
             }
         }

         IEnumerator IEnumerable.GetEnumerator()
         {
             throw new NotImplementedException();
         }

         public IHtmlString PageBar(string url, object property)
         {
             if (string.IsNullOrEmpty(url)) url = string.Empty;
             else
             {
                 url = url.Trim().TrimEnd('?', '&');
                 if (url.Contains("?")) url += "&";
                 else url += "?";
             }
             string html = "<ul class=\"pagination\">{0}</ul>";
             , pageMax = this.TotalPage;
             string page = string.Empty;
             ; (i > ()) && i > ; i--)
             {
                 string pageUrl = string.Format("{0}pageIndex={1}", url, i);
                 page = "<li><a href=\"" + pageUrl + "\">" + i + "</a></li>" + page;
                 pageMin = i;
             }
             page = page + "<li class=\"active\"><a href=\"#\">" + this.pageIndex + "</a></li>";
             ; (i < ()) && i < ; i++)
             {
                 string pageUrl = string.Format("{0}pageIndex={1}", url, i);
                 page = page + "<li><a href=\"" + pageUrl + "\">" + i + "</a></li>";
                 pageMax = i;
             }
             //第一页和最后一页
             )
             {
                 ) > )
                     page = "<li><a>...</a></li>" + page;
                 );
                 page =  + "</a></li>" + page;
             }
             if (pageMax < this.TotalPage)
             {
                 )
                     page = page + "<li><a>...</a></li>";
                 string pageUrl = string.Format("{0}pageIndex={1}", url, this.TotalPage);
                 page = page + "<li><a href=\"" + pageUrl + "\">" + this.TotalPage + "</a></li>";
             }
             //上一页和下一页
             )
                 page = "<li class=\"prev disabled\"><a href=\"#\"><span class=\"fa fa-angle-left\"></span>&nbsp;上一页</a></li>" + page;
             else
                 page = ) + "\"><span class=\"fa fa-angle-left\"></span>&nbsp;上一页</a></li>" + page;
             if (this.pageIndex == this.TotalPage)
                 page = page + "<li class=\"next disabled\"><a href=\"#\">下一页&nbsp;<span class=\"fa fa-angle-right\"></span></a></li>";
             else
                 page = page + ) + "\">下一页&nbsp;<span class=\"fa fa-angle-right\"></span></a></li>";
             html = string.Format(html, page);
             return new HtmlString(html);
         }
     }
 }
05-11 13:06