C#
1 /// <summary> 2 /// 翻页 3 /// </summary> 4 using System.Text; 5 public class PageTurning 6 { 7 #region 构造函数 8 /// <summary> 9 /// 翻页 10 /// </summary> 11 /// <param name="count">页码总量</param> 12 /// <param name="showCount">显示数量</param> 13 /// <param name="index">当前页码</param> 14 /// <param name="element">显示元素</param> 15 /// <param name="css">默认样式</param> 16 /// <param name="cssChecked">选中样式</param> 17 /// <param name="cssPageTurning">上一页下一页样式</param> 18 /// <param name="click">翻页事件</param> 19 public PageTurning(int count, int showCount, int index, string element, string css, string cssChecked, string cssPageTurning, string click) 20 { 21 this.Count = count; 22 this.ShowCount = showCount; 23 this.Index = index; 24 this.Element = element; 25 this.Css = css; 26 this.CssChecked = cssChecked; 27 this.CssPageTurning = cssPageTurning; 28 this.Click = click; 29 } 30 #endregion 31 32 #region 参数 33 #region 基本参数 34 /// <summary> 35 /// 当前页码 36 /// </summary> 37 public int Index { get; set; } 38 /// <summary> 39 /// 页码总量 40 /// </summary> 41 public int Count { get; set; } 42 /// <summary> 43 /// 显示数量 44 /// </summary> 45 public int ShowCount { get; set; } 46 #endregion 47 48 #region HTML参数 49 /// <summary> 50 /// 显示元素 51 /// </summary> 52 public string Element { get; set; } 53 /// <summary> 54 /// 默认样式 55 /// </summary> 56 public string Css { get; set; } 57 /// <summary> 58 /// 选中样式 59 /// </summary> 60 public string CssChecked { get; set; } 61 /// <summary> 62 /// 上一页下一页样式 63 /// </summary> 64 public string CssPageTurning { get; set; } 65 /// <summary> 66 /// 翻页事件 67 /// </summary> 68 public string Click { get; set; } 69 #endregion 70 #endregion 71 72 #region 获取最大最小值 73 /// <summary> 74 /// 获取最大最小值 75 /// </summary> 76 /// <returns></returns> 77 private int[] GetMinMax() 78 { 79 int[] minMax = new int[2]; 80 if (Index < ShowCount + 2)//123... 81 { 82 minMax[0] = 1; 83 minMax[1] = Count > (ShowCount * 2 + 1) ? (ShowCount * 2 + 1) : Count; 84 } 85 else if (Index > Count - 2 * ShowCount + 1)//...678 86 { 87 minMax[0] = Count - 2 * ShowCount; 88 minMax[1] = Count; 89 } 90 else//...345... 91 { 92 minMax[0] = Index - ShowCount; 93 minMax[1] = Index + ShowCount; 94 } 95 //防止出错 96 minMax[0] = minMax[0] < 1 ? 1 : minMax[0]; 97 minMax[1] = minMax[1] < Count ? minMax[1] : Count; 98 minMax[1]++; 99 return minMax; 100 } 101 #endregion 102 103 #region 生成HTML 104 /// <summary> 105 /// 生成HTML 106 /// </summary> 107 /// <returns></returns> 108 public string Building() 109 { 110 var minMax = GetMinMax(); 111 StringBuilder sb = new StringBuilder(); 112 if (Index > 1)//加1... 113 { 114 sb.AppendFormat("<{0} class={1} onclick={2}>上一页</{0}>", Element, CssPageTurning, Click + "(" + (Index > 1 ? Index - 1 : 1) + ")"); 115 if (minMax[0] > 1) 116 { 117 sb.AppendFormat("<{0} class={1} onclick={2}>1</{0}>", Element, Css, Click + "(1)"); 118 if (minMax[0] > 2) 119 { 120 sb.AppendFormat("<{0} class={1}>...</{0}>", Element, Css); 121 } 122 } 123 } 124 while (minMax[0] < minMax[1])//1...345...7 125 { 126 sb.AppendFormat("<{0} class={1} onclick={2}>{3}</{0}>", Element, minMax[0] != Index ? Css : CssChecked, minMax[0] != Index ? Click + "(" + minMax[0] + ")" : "", minMax[0]); 127 minMax[0]++; 128 } 129 if (Index < Count)//加..7 130 { 131 if (minMax[1] < Count + 1) 132 { 133 if (minMax[1] < Count) 134 { 135 sb.AppendFormat("<{0} class={1}>...</{0}>", Element, Css); 136 } 137 sb.AppendFormat("<{0} class={1} onclick={2}>{3}</{0}>", Element, Css, Click + "(" + Count + ")", Count); 138 } 139 sb.AppendFormat("<{0} class={1} onclick={2}>下一页</{0}>", Element, CssPageTurning, Click + "(" + (Count > Index ? Index + 1 : Count) + ")"); 140 } 141 return sb.ToString(); 142 } 143 #endregion 144 }
调用
public string get(int index)
{
PageTurning pt = new PageTurning(20,1,index,"a","aa","bb","cc","aaaaa");
return pt.Building();
}
HTML实例
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta name="viewport" content="width=device-width" /> 5 <title>Index</title> 6 <script src="~/Scripts/jquery-1.8.2.min.js"></script> 7 <script> 8 $(function () { 9 aaaaa(22) 10 }) 11 function aaaaa(pIndex) { 12 $("div").load("/test/get", { index: pIndex }); 13 } 14 </script> 15 <style> 16 .aa { 17 cursor: pointer; 18 display: block; 19 border: 1px solid black; 20 width: 22px; 21 height: 24px; 22 float: left; 23 margin-left: 4px; 24 } 25 26 .bb { 27 cursor: pointer; 28 display: block; 29 border: 1px solid black; 30 width: 22px; 31 height: 24px; 32 float: left; 33 margin-left: 4px; 34 background-color: pink; 35 } 36 37 .cc { 38 cursor: pointer; 39 display: block; 40 border: 1px solid black; 41 width: 60px; 42 height: 24px; 43 float: left; 44 margin-left: 4px; 45 background-color: orange; 46 } 47 </style> 48 </head> 49 <body> 50 <div></div> 51 </body> 52 </html>
效果图
时间: 2024-10-17 22:34:57