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; if (totalCount % pageSize > 0) this.totalPage += 1; 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; if (list.Count % pageSize > 0) this.totalPage += 1; this.list = list.Skip(pageSize * (pageIndex - 1)).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>"; int pageMin = 1, pageMax = this.TotalPage; string page = string.Empty; for (int i = this.PageIndex - 1; (i > (this.PageIndex - 5)) && i > 0; 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>"; for (int i = this.PageIndex + 1; (i < (this.PageIndex + 5)) && i < this.TotalPage + 1; i++) { string pageUrl = string.Format("{0}pageIndex={1}", url, i); page = page + "<li><a href="" + pageUrl + "">" + i + "</a></li>"; pageMax = i; } //第一页和最后一页 if (pageMin > 1) { if ((pageMin - 1) > 1) page = "<li><a>...</a></li>" + page; string pageUrl = string.Format("{0}pageIndex={1}", url, 1); page = "<li><a href="" + pageUrl + "">" + 1 + "</a></li>" + page; } if (pageMax < this.TotalPage) { if ((this.TotalPage - pageMax) > 1) 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>"; } //上一页和下一页 if (this.pageIndex == 1) page = "<li class="prev disabled"><a href="#"><span class="fa fa-angle-left"></span> 上一页</a></li>" + page; else page = "<li class="prev"><a href="" + string.Format("{0}pageIndex={1}", url, this.pageIndex - 1) + ""><span class="fa fa-angle-left"></span> 上一页</a></li>" + page; if (this.pageIndex == this.TotalPage) page = page + "<li class="next disabled"><a href="#">下一页 <span class="fa fa-angle-right"></span></a></li>"; else page = page + "<li class="next"><a href="" + string.Format("{0}pageIndex={1}", url, this.pageIndex + 1) + "">下一页 <span class="fa fa-angle-right"></span></a></li>"; html = string.Format(html, page); return new HtmlString(html); } } }