namespace Szmedi.RvKits.FamilyTools { public class PageInfo { private readonly int pageSize; public List dataSource; public int CurrentIndex { get; set; } public int Pagecount { get; set; } public PageInfo() { //当前页码 CurrentIndex = 1; } public PageInfo(List dataSource, int pageSize) : this() { this.dataSource = dataSource; //每页的数目 this.pageSize = pageSize; //取商 Pagecount = dataSource.Count / pageSize; //如果整除+0,不整除+1 Pagecount += dataSource.Count % pageSize != 0 ? 1 : 0; } public List GetPageData(JumpOperation jo) { switch (jo) { case JumpOperation.GoHome: CurrentIndex = 1; break; case JumpOperation.GoPrePrevious: if (CurrentIndex > 1) { CurrentIndex -= 1; } break; case JumpOperation.GoNext: if (CurrentIndex < Pagecount) { CurrentIndex += 1; } break; case JumpOperation.GoEnd: CurrentIndex = Pagecount; break; case JumpOperation.GoAny: break; } List listPageData = new(); try { int pageCountTo = pageSize; //页数等于1并且取余大于0(即只有一页时) if (Pagecount == CurrentIndex && dataSource.Count % pageSize > 0) { //pageCountTo=当前条目数 pageCountTo = dataSource.Count % pageSize; } if (null != dataSource) { for (int i = 0; i < pageCountTo; i++) { //当前页码-1进行循环,比如有32项时,当前第2页,即从Index=20(第一项Index=0)开始循环 if (((CurrentIndex - 1) * pageSize) + i < dataSource.Count) { //将该项添加到listPageData listPageData.Add(dataSource[((CurrentIndex - 1) * pageSize) + i]); } else { break; } } } return listPageData; } catch { return listPageData; } } } public enum JumpOperation { GoHome = 0, GoPrePrevious = 1, GoNext = 2, GoEnd = 3, GoAny = 4 } }