1 /// <summary> 2 /// 生成分页页码 3 /// </summary> 4 /// <param name="total">总数</param> 5 /// <param name="pagesize">页大小</param> 6 /// <param name="index">当前页</param> 7 /// <param name="length">返回页码个数</param> 8 /// <returns></returns> 9 public static int[] PageSplit(int total,int pagesize,int index,int length) 10 { 11 //总页数 12 var pagetotal = total / pagesize; 13 if (total % pagesize > 0) { pagetotal++; } 14 //当前页校正 15 if (index < 1) { index = 1; } 16 if (index > pagetotal) { index = pagetotal; } 17 18 var listSplit = new List<int>(); 19 20 var len = length; 21 if (len % 2 == 0) { len++; } 22 var half = len / 2; 23 24 var start = index - half; 25 var end = index + half; 26 27 if (start < 1) 28 { 29 var p = 1 - start; 30 start += p; 31 end += p; 32 } 33 if (end > pagetotal) 34 { 35 var p = end - pagetotal; 36 end = pagetotal; 37 if (start - p >= 1) { start -= p; } 38 else { start = 1; } 39 } 40 for (int i = start, j = 0; i <= end && j < length; i++, j++) 41 { 42 listSplit.Add(i); 43 } 44 return listSplit.ToArray(); 45 }
完整代码:
1 namespace Common 2 { 3 /// <summary> 4 /// web端分页 5 /// </summary> 6 public class PageSplit 7 { 8 /// <summary> 9 /// 初始化 10 /// </summary> 11 /// <param name="totalRecoord">总记录数</param> 12 /// <param name="currentPage">当前页</param> 13 /// <param name="pageSize">页大小</param> 14 public PageSplit(int totalRecoord,int currentPage,int pageSize) 15 { 16 TotalRecord = totalRecoord; 17 CurrentPage = currentPage; 18 PageSize = pageSize; 19 20 TotalPage = GetTotalPage(totalRecoord, pageSize); 21 if ((currentPage - 1) < 1) { PrevPage = 1; } 22 else { PrevPage = currentPage - 1; } 23 if ((currentPage + 1) > TotalPage) { NextPage = TotalPage; } 24 else { NextPage = currentPage + 1; } 25 } 26 27 #region 初始化属性 28 /// <summary> 总记录数 </summary> 29 public int TotalRecord { get; private set; } 30 /// <summary> 当前页 </summary> 31 public int CurrentPage { get; private set; } 32 /// <summary> 页大小 </summary> 33 public int PageSize { get; private set; } 34 /// <summary> 上一页 </summary> 35 public int PrevPage { get; private set; } 36 /// <summary> 下一页 </summary> 37 public int NextPage { get; private set; } 38 /// <summary> 总页数 </summary> 39 public int TotalPage { get; private set; } 40 #endregion 41 42 #region 可设置属性 43 private int SplitLength = 5; 44 private string _ActiveTemplate = "<a class="active" href="{pageindex}"></a>"; 45 private string _DefaultTemplate = "<a href="{pageindex}"></a>"; 46 47 /// <summary> 设置页码数量 </summary> 48 public int SetSplitLength { get { return SplitLength; } set { SplitLength = value; } } 49 /// <summary> 活动页模板 </summary> 50 public string ActiveTemplate { get { return _ActiveTemplate; } set { _ActiveTemplate = value; } } 51 /// <summary> 普通页模板 </summary> 52 public string DefaultTemplate { get { return _DefaultTemplate; } set { _DefaultTemplate = value; } } 53 #endregion 54 55 /// <summary> 56 /// 分页Html代码 57 /// </summary> 58 public string SplitHtml 59 { 60 get 61 { 62 StringBuilder sbHtml = new StringBuilder(); 63 var pages = Split(TotalRecord, PageSize, CurrentPage, SplitLength); 64 foreach (var index in pages) 65 { 66 if (index == CurrentPage) { sbHtml.Append(ActiveTemplate.ToLower().Replace("{pageindex}", index.ToString())); } 67 else { sbHtml.Append(DefaultTemplate.ToLower().Replace("{pageindex}", index.ToString())); } 68 } 69 return sbHtml.ToString(); 70 } 71 } 72 73 #region 方法 74 /// <summary> 75 /// 获取总页数 76 /// </summary> 77 /// <param name="totalRecord"></param> 78 /// <param name="pageSize"></param> 79 /// <returns></returns> 80 private static int GetTotalPage(int totalRecord,int pageSize) 81 { 82 var pagetotal = totalRecord / pageSize; 83 if (totalRecord % pageSize > 0) { pagetotal++; } 84 return pagetotal; 85 } 86 /// <summary> 87 /// 生成分页页码 88 /// </summary> 89 /// <param name="total">总数</param> 90 /// <param name="pagesize">页大小</param> 91 /// <param name="index">当前页</param> 92 /// <param name="length">返回页码个数</param> 93 /// <returns></returns> 94 public static int[] Split(int total, int pagesize, int index, int length) 95 { 96 //总页数 97 var pagetotal = GetTotalPage(total,pagesize); 98 //当前页校正 99 if (index < 1) { index = 1; } 100 if (index > pagetotal) { index = pagetotal; } 101 102 var listSplit = new List<int>(); 103 104 var len = length; 105 if (len % 2 == 0) { len++; } 106 var half = len / 2; 107 108 var start = index - half; 109 var end = index + half; 110 111 if (start < 1) 112 { 113 var p = 1 - start; 114 start += p; 115 end += p; 116 } 117 if (end > pagetotal) 118 { 119 var p = end - pagetotal; 120 end = pagetotal; 121 if (start - p >= 1) { start -= p; } 122 else { start = 1; } 123 } 124 for (int i = start, j = 0; i <= end && j < length; i++, j++) 125 { 126 listSplit.Add(i); 127 } 128 return listSplit.ToArray(); 129 } 130 #endregion 131 } 132 }