You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Application/PagedList.cs

93 lines
2.4 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Infrastructure.Application
{
public class PagedList<T> : IPagedList, IValidatableObject
{
[ScaffoldColumn(false)]
public int DefaultPageSize { get; set; } = 20;
public PagedList()
{
PageIndex = 1;
PageSize = this.DefaultPageSize;
}
[ScaffoldColumn(false)]
public int PageIndex { get; set; }
[ScaffoldColumn(false)]
public int PageSize { get; set; }
[ScaffoldColumn(false)]
public int TotalCount { get; set; }
public int[] GetPageSizes()
{
return new int[] { 20, 50, 100 };
}
public int PageCount()
{
return (int)Math.Ceiling(this.TotalCount / (double)this.PageSize);
}
public bool HasPrev()
{
return PageIndex > 1;
}
public bool HasNext()
{
return PageIndex < PageCount();
}
public IEnumerable<int> GetPageIndexs()
{
var pageCount = PageCount();
var maxLinks = 10;
var left = maxLinks / 2;
var start = PageIndex - left;
start = start > 1 ? start : 1;
var end = PageIndex + (maxLinks - left - 1);
end = end > pageCount ? pageCount : end;
if (start == 1 && end < pageCount)
{
while (end - start + 1 < maxLinks && end < pageCount)
{
end++;
}
}
if (end == pageCount && start > 1)
{
while (end - start + 1 < maxLinks && start > 1)
{
start--;
}
}
var list = new List<int>();
for (int i = start; i <= end; i++)
{
list.Add(i);
}
return list;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (PageIndex < 1)
{
yield return new ValidationResult("当前页索引必须大于等于1");
}
if (PageSize > 1000)
{
yield return new ValidationResult("分页数量超出系统限制");
}
}
[ScaffoldColumn(false)]
public List<T> List { get; } = new List<T>();
}
}