Files
SzmediTools/Szmedi.RvKits/FamilyTools/PageInfo.cs
2025-09-16 16:06:41 +08:00

103 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace Szmedi.RvKits.FamilyTools
{
public class PageInfo<T>
{
private readonly int pageSize;
public List<T> dataSource;
public int CurrentIndex { get; set; }
public int Pagecount { get; set; }
public PageInfo()
{
//当前页码
CurrentIndex = 1;
}
public PageInfo(List<T> 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<T> 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<T> 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
}
}