1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System.Drawing.Printing;
- namespace AipDatabase.API.Models
- {
- public class Pagination<T>
- {
- public int page { get; set; }
- public int pageSize { get; set; }
- public int pagePerCount { get; set; }
- public int totalCount { get; set; }
- public int currentPage { get; set; }
- public int totalPageCount { get; set; }
- public int startPage { get; set; }
- public int endPage { get; set; }
- public int startRow { get; set; }
- public int endRow { get; set; }
- public int prev { get; set; }
- public int next { get; set; }
- public List<T> lists { get; set; }
- public Pagination(int page, int pageSize, int pagePerCount, int totalCount)
- {
- this.page = page;
- this.pageSize = pageSize;
- this.pagePerCount = pagePerCount;
- this.totalCount = totalCount;
- this.currentPage = this.page;
- this.totalPageCount = this.getTotalPageCount(this.totalCount, this.pagePerCount);
- this.startPage = this.getStartPage(this.page, this.pageSize);
- this.endPage = this.getEndPage(this.startPage, this.totalPageCount, this.pageSize);
- this.startRow = this.getStartRow(this.page, this.pagePerCount);
- this.endRow = this.getEndRow(this.startRow, this.pagePerCount, this.totalCount);
- this.prev = this.getPrev(this.pageSize, this.startPage);
- this.next = this.getNext(this.endPage, this.totalPageCount);
- this.lists = new List<T>();
- }
- private int getStartPage(int page, int pageSize)
- {
- int start = (int)Math.Floor((double)page / pageSize);
- if (page % pageSize == 0)
- {
- start--;
- }
- return (start * pageSize) + 1;
- }
- private int getEndPage(int startPage, int totalPageCount, int pageSize)
- {
- int endPage = (startPage - 1) + pageSize;
- if (endPage > totalPageCount)
- {
- endPage = totalPageCount;
- }
- return endPage;
- }
- private int getStartRow(int page, int pagePerCount)
- {
- return ((page - 1) * pagePerCount) + 1;
- }
- private int getEndRow(int startRow, int pagePerCount, int totalCount)
- {
- int endRow = startRow + pagePerCount - 1;
- if (endRow > totalCount)
- {
- endRow = totalCount;
- }
- return endRow;
- }
- private int getPrev(int pageSize, int startPage)
- {
- return (pageSize < startPage) ? (startPage - pageSize) : 0;
- }
- private int getNext(int endPage, int totalPageCount)
- {
- return (endPage + 1 <= totalPageCount) ? (endPage + 1) : 0;
- }
- private int getTotalPageCount(int totalCount, int pagePerCount)
- {
- return (int)Math.Ceiling(totalCount * 1.0 / pagePerCount);
- }
- }
- }
|