Pagination.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Drawing.Printing;
  2. namespace AipDatabase.API.Models
  3. {
  4. public class Pagination<T>
  5. {
  6. public int page { get; set; }
  7. public int pageSize { get; set; }
  8. public int pagePerCount { get; set; }
  9. public int totalCount { get; set; }
  10. public int currentPage { get; set; }
  11. public int totalPageCount { get; set; }
  12. public int startPage { get; set; }
  13. public int endPage { get; set; }
  14. public int startRow { get; set; }
  15. public int endRow { get; set; }
  16. public int prev { get; set; }
  17. public int next { get; set; }
  18. public List<T> lists { get; set; }
  19. public Pagination(int page, int pageSize, int pagePerCount, int totalCount)
  20. {
  21. this.page = page;
  22. this.pageSize = pageSize;
  23. this.pagePerCount = pagePerCount;
  24. this.totalCount = totalCount;
  25. this.currentPage = this.page;
  26. this.totalPageCount = this.getTotalPageCount(this.totalCount, this.pagePerCount);
  27. this.startPage = this.getStartPage(this.page, this.pageSize);
  28. this.endPage = this.getEndPage(this.startPage, this.totalPageCount, this.pageSize);
  29. this.startRow = this.getStartRow(this.page, this.pagePerCount);
  30. this.endRow = this.getEndRow(this.startRow, this.pagePerCount, this.totalCount);
  31. this.prev = this.getPrev(this.pageSize, this.startPage);
  32. this.next = this.getNext(this.endPage, this.totalPageCount);
  33. this.lists = new List<T>();
  34. }
  35. private int getStartPage(int page, int pageSize)
  36. {
  37. int start = (int)Math.Floor((double)page / pageSize);
  38. if (page % pageSize == 0)
  39. {
  40. start--;
  41. }
  42. return (start * pageSize) + 1;
  43. }
  44. private int getEndPage(int startPage, int totalPageCount, int pageSize)
  45. {
  46. int endPage = (startPage - 1) + pageSize;
  47. if (endPage > totalPageCount)
  48. {
  49. endPage = totalPageCount;
  50. }
  51. return endPage;
  52. }
  53. private int getStartRow(int page, int pagePerCount)
  54. {
  55. return ((page - 1) * pagePerCount) + 1;
  56. }
  57. private int getEndRow(int startRow, int pagePerCount, int totalCount)
  58. {
  59. int endRow = startRow + pagePerCount - 1;
  60. if (endRow > totalCount)
  61. {
  62. endRow = totalCount;
  63. }
  64. return endRow;
  65. }
  66. private int getPrev(int pageSize, int startPage)
  67. {
  68. return (pageSize < startPage) ? (startPage - pageSize) : 0;
  69. }
  70. private int getNext(int endPage, int totalPageCount)
  71. {
  72. return (endPage + 1 <= totalPageCount) ? (endPage + 1) : 0;
  73. }
  74. private int getTotalPageCount(int totalCount, int pagePerCount)
  75. {
  76. return (int)Math.Ceiling(totalCount * 1.0 / pagePerCount);
  77. }
  78. }
  79. }