标签:分页控件 class blog c code java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 |
using
System; using
System.Collections.Generic; using
System.Linq; using
System.Web; namespace
MvcTest.Extends { /// <summary> /// 分页元素位置 /// </summary> public
enum PagerElementPosition { Left, Right } public
class PagerConfig { /// <summary> /// 记录总条数 /// </summary> public
int TotalRecord { get ; set ; } /// <summary> /// 记录的单位,默认为“条” /// </summary> public
string RecordUnit { get ; set ; } /// <summary> /// 记录的名称,默认为“记录” /// </summary> public
string RecordName { get ; set ; } /// <summary> /// 当前页码的参数名 /// </summary> public
string CurrentPageKey { get ; set ; } /// <summary> /// 当前页码 只读 /// </summary> public
int CurrentPage { get { if
(HttpContext.Current.Request.Params[CurrentPageKey] == null ) { return
1; } else { try { int
currentPage = Convert.ToInt32(HttpContext.Current.Request.Params[CurrentPageKey]); if
(currentPage < 1) { return
1; } else
if (currentPage > TotalPage) { return
TotalPage; } else { return
currentPage; } } catch { return
1; } } } } private
int _PageSize; /// <summary> /// 每页显示记录数 /// </summary> public
int PageSize { get { return
_PageSize; } set { if
(value < 1) { _PageSize = 1; } else { _PageSize = value; } } } /// <summary> /// 总页数 只读 /// </summary> public
int TotalPage { get { return
( int )Math.Ceiling(TotalRecord / ( double )PageSize); } } /// <summary> /// 是否显示首页、尾页链接 /// </summary> public
bool ShowFirstLastPageLink { get ; set ; } /// <summary> /// 是否显示上一页、下一页链接 /// </summary> public
bool ShowPrevNextPageLink { get ; set ; } /// <summary> /// 是否显示数字按钮 /// </summary> public
bool ShowDigitalLink { get ; set ; } /// <summary> /// 数字按钮数量 /// </summary> public
int DigitalLinkCount { get ; set ; } /// <summary> /// 是否显示总记录数 /// </summary> public
bool ShowTotalRecord { get ; set ; } /// <summary> /// 总记录数出现位置 /// </summary> public
PagerElementPosition TotalRecordPosition { get ; set ; } /// <summary> /// 是否显示当前页数和总页数信息 /// </summary> public
bool ShowPageInfo { get ; set ; } /// <summary> /// 当前页和总页数信息显示位置 /// </summary> public
PagerElementPosition PageInfoPosition { get ; set ; } /// <summary> /// 是否显示GoTo输入区域 /// </summary> public
bool ShowGoTo { get ; set ; } /// <summary> /// 指定生成的元素对应的class的前缀字符 /// </summary> public
string CssClassPreWord { get ; set ; } /// <summary> /// 是否创建为ajax分页控件 /// </summary> public
bool UseAjax { get ; set ; } /// <summary> /// Ajax提交后更新的html元素id /// </summary> public
string AjaxUpdateTargetID { get ; set ; } /// <summary> /// Ajax提交后调用的js function名称 /// </summary> public
string AjaxSuccessFunctionName { get ; set ; } /// <summary> /// 是否自动生成Ajax提交后调用的js function /// </summary> public
bool AutoGenarateAjaxSuccessFunction { get ; set ; } /// <summary> /// 使用默认值初始化设置 /// </summary> public
PagerConfig() { PageSize = 4; RecordUnit = "条" ; RecordName = "记录" ; CurrentPageKey = "page" ; CssClassPreWord = "pager" ; ShowFirstLastPageLink = true ; ShowPrevNextPageLink = true ; ShowDigitalLink = true ; DigitalLinkCount = 10; ShowTotalRecord = false ; TotalRecordPosition = PagerElementPosition.Left; ShowPageInfo = false ; PageInfoPosition = PagerElementPosition.Left; ShowGoTo = false ; UseAjax = false ; AjaxUpdateTargetID = "" ; AjaxSuccessFunctionName = "OnPageChanged" ; AutoGenarateAjaxSuccessFunction = true ; } } /// <summary> /// 配合Pager扩展实现分页的帮助类 /// </summary> public
class PagerHelper { /// <summary> /// 获取记录开始和结束编号,并返回实际的页码 /// </summary> /// <param name="allCount">记录总条数</param> /// <param name="pageSize">页大小</param> /// <param name="pageIndex">(输出)当前页码</param> /// <param name="startIndex">(输出)开始编号</param> /// <param name="endIndex">(输出)结束编号</param> /// <param name="currentPageKey">分页参数名称</param> /// <param name="pageIndexIs0Based">页码编号是否从0开始,默认为否</param> /// <param name="recordIndexIs0Based">记录编号是否从0开始,默认为否</param> public
static void GetStartAndEndIndex( int
allCount, int
pageSize, out
int pageIndex, out
int startIndex, out
int endIndex, string
currentPageKey = "page" , bool
pageIndexIs0Based = false , bool
recordIndexIs0Based = false ) { //计算pageIndex的实际值 pageIndex = GetRealPageIndex(allCount, pageSize, currentPageKey, pageIndexIs0Based); //计算过程是0based的 if
(!pageIndexIs0Based) { pageIndex--; //转成0based } //计算startIndex和endIndex startIndex = pageIndex * pageSize; endIndex = startIndex + pageSize - 1; if
(endIndex > allCount - 1) { endIndex = allCount - 1; } //0based计算完成,下面根据设置不同,输出不同 if
(!pageIndexIs0Based) { pageIndex++; } if
(!recordIndexIs0Based) { startIndex++; endIndex++; } } /// <summary> /// 返回实际页码 /// </summary> /// <param name="allCount">总记录数</param> /// <param name="pageSize">页面大小</param> /// <param name="currentPageKey">分页参数名称</param> /// <param name="pageIndexIs0Based">页码编号是否从0开始,默认为否</param> /// <returns>实际页码</returns> public
static int GetRealPageIndex( int
allCount, int
pageSize, string
currentPageKey = "page" , bool
pageIndexIs0Based = false ) { int
pageIndex; //整个计算过程都是0based的 if
(pageSize < 1) { pageSize = 1; //容错 } if
(HttpContext.Current.Request.Params[currentPageKey] == null ) { pageIndex = 0; } else { try { int
_pageIndex = Convert.ToInt32(HttpContext.Current.Request.Params[currentPageKey]); //待判断的页码 if
(!pageIndexIs0Based) { _pageIndex--; //转成0based } if
(_pageIndex < 0) { pageIndex = 0; } else { int
totalPage = ( int )Math.Ceiling(allCount / ( double )pageSize); if
(_pageIndex >= totalPage) { pageIndex = totalPage - 1; } else { pageIndex = _pageIndex; } } } catch { pageIndex = 0; } } //0based计算完成,下面根据设置不同,输出不同 return
(pageIndexIs0Based) ? pageIndex : pageIndex + 1; } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 |
using
MvcTest.Extends; using
System; using
System.Collections.Generic; using
System.Linq; using
System.Text; using
System.Web; using
System.Web.Mvc; using
System.Web.Routing; namespace
MvcTest.HTML { public
static class PagerExtensions { //提取 返回a标签 方法 private
static string getLinkHtml(UrlHelper urlHelper, bool
useAjax, string
ajaxSuccessFunction, string
linkContent, string
actionName, string
controllerName, RouteValueDictionary routeValues) { string
link = "" ; if
(useAjax) { link += "<a href=\"javascript:$.post(‘"
+ urlHelper.Action(actionName, controllerName) + "‘,{" ; //将route放到post表单中 foreach
( var
route in
routeValues.Keys) { link += route + ":‘"
+ routeValues[route].ToString() + "‘," ; } if
(routeValues.Count > 0) { link = link.Remove(link.Length - 1); } link += "},"
+ ajaxSuccessFunction + ")\" >" ; } else { link += "<a href=\""
+ urlHelper.Action(actionName, controllerName, routeValues) + "\">" ; } link += linkContent; link += "</a>" ; return
link; } #region 分页扩展 /// <summary> /// 返回用于分页的div元素 /// </summary> /// <param name="htmlHelper">HtmlHelper</param> /// <param name="pagerConfig">分页设置对象</param> /// <returns></returns> public
static MvcHtmlString Pager( this
HtmlHelper htmlHelper, PagerConfig pagerConfig) { return
Pager(htmlHelper, "" , "" , new
{ }, new
{ }, pagerConfig); } /// <summary> /// 返回用于分页的div元素 /// </summary> /// <param name="htmlHelper">HtmlHelper</param> /// <param name="htmlAttributes">html属性对象</param> /// <param name="pagerConfig">分页设置对象</param> /// <returns></returns> public
static MvcHtmlString Pager( this
HtmlHelper htmlHelper, object
htmlAttributes, PagerConfig pagerConfig) { return
Pager(htmlHelper, "" , "" , new
{ }, htmlAttributes, pagerConfig); } /// <summary> /// 返回用于分页的div元素 /// </summary> /// <param name="htmlHelper">HtmlHelper</param> /// <param name="actionName">方法</param> /// <param name="htmlAttributes">html属性对象</param> /// <param name="pagerConfig">分页设置对象</param> /// <returns></returns> public
static MvcHtmlString Pager( this
HtmlHelper htmlHelper, string
actionName, object
htmlAttributes, PagerConfig pagerConfig) { return
Pager(htmlHelper, actionName, "" , new
{ }, htmlAttributes, pagerConfig); } /// <summary> /// 返回用于分页的div元素 /// </summary> /// <param name="htmlHelper">HtmlHelper</param> /// <param name="actionName">方法</param> /// <param name="controllerName">控制器</param> /// <param name="htmlAttributes">html属性对象</param> /// <param name="pagerConfig">分页设置对象</param> /// <returns></returns> public
static MvcHtmlString Pager( this
HtmlHelper htmlHelper, string
actionName, string
controllerName, object
htmlAttributes, PagerConfig pagerConfig) { return
Pager(htmlHelper, actionName, controllerName, new
{ }, htmlAttributes, pagerConfig); } /// <summary> /// 返回用于分页的div元素 /// </summary> /// <param name="htmlHelper">HtmlHelper</param> /// <param name="actionName">方法</param> /// <param name="controllerName">控制器</param> /// <param name="routeValues">路由参数</param> /// <param name="htmlAttributes">html属性对象</param> /// <param name="pagerConfig">分页设置对象</param> /// <returns></returns> public
static MvcHtmlString Pager( this
HtmlHelper htmlHelper, string
actionName, string
controllerName, object
routeValues, object
htmlAttributes, PagerConfig pagerConfig) { RouteValueDictionary RouteValues; if
(routeValues == null ) { RouteValues = new
RouteValueDictionary(); } else { RouteValues = new
RouteValueDictionary(routeValues); } UrlHelper Url = new
UrlHelper(htmlHelper.ViewContext.RequestContext); AjaxHelper Ajax = new
AjaxHelper(htmlHelper.ViewContext, htmlHelper.ViewDataContainer); StringBuilder sbPager = new
StringBuilder(); sbPager.Append( "<div" ); //利用反射获取htmlAttributes的全部元素和值 if
(htmlAttributes != null ) { System.Reflection.PropertyInfo[] htmlProperties = (System.Reflection.PropertyInfo[])htmlAttributes.GetType().GetProperties(); foreach
( var
property in
htmlProperties) { sbPager.Append( " "
+ property.Name + "=\""
+ property.GetValue(htmlAttributes).ToString() + "\"" ); } } sbPager.Append( ">" ); //左侧记录总数信息 if
(pagerConfig.ShowTotalRecord && pagerConfig.PageInfoPosition == PagerElementPosition.Left) { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-count\">共"
+ pagerConfig.TotalRecord + pagerConfig.RecordUnit + pagerConfig.RecordName + "</span>" ); } //左侧页码信息 if
(pagerConfig.ShowPageInfo && pagerConfig.PageInfoPosition == PagerElementPosition.Left) { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-info\">第"
+ pagerConfig.CurrentPage + "页/共"
+ pagerConfig.TotalPage + "页</span>" ); } //首页 if
(pagerConfig.ShowFirstLastPageLink) { if
(pagerConfig.CurrentPage > 1) { RouteValues[pagerConfig.CurrentPageKey] = 1; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">"
+ getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "首页" , actionName, controllerName, RouteValues) + "</span>" ); } else { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">首页</span>" ); } } //上一页 if
(pagerConfig.ShowPrevNextPageLink) { if
(pagerConfig.CurrentPage > 1) { RouteValues[pagerConfig.CurrentPageKey] = pagerConfig.CurrentPage - 1; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">"
+ getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "上一页" , actionName, controllerName, RouteValues) + "</span>" ); } else { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">上一页</span>" ); } } //数字导航开始 if
(pagerConfig.ShowDigitalLink) { int
shownStartPageIndex, shownEndPageIndex; //总页数少于要显示的页数,页码全部显示 if
(pagerConfig.DigitalLinkCount >= pagerConfig.TotalPage) { shownStartPageIndex = 1; shownEndPageIndex = pagerConfig.TotalPage; } else //显示指定数量的页码 { int
forward = ( int )Math.Ceiling(pagerConfig.DigitalLinkCount / 2.0); if
(pagerConfig.CurrentPage > forward) //起始页码大于1 { shownEndPageIndex = pagerConfig.CurrentPage + pagerConfig.DigitalLinkCount - forward; if
(shownEndPageIndex > pagerConfig.TotalPage) //结束页码大于总页码结束页码为最后一页 { shownStartPageIndex = pagerConfig.TotalPage - pagerConfig.DigitalLinkCount + 1; shownEndPageIndex = pagerConfig.TotalPage; } else { shownStartPageIndex = pagerConfig.CurrentPage - forward + 1; } } else //起始页码从1开始 { shownStartPageIndex = 1; shownEndPageIndex = pagerConfig.DigitalLinkCount; } } //向上… if
(shownStartPageIndex > 1) { RouteValues[pagerConfig.CurrentPageKey] = shownStartPageIndex - 1; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-number\">"
+ getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "..." , actionName, controllerName, RouteValues) + "</span>" ); } //数字 for
( int
i = shownStartPageIndex; i <= shownEndPageIndex; i++) { if
(i != pagerConfig.CurrentPage) { RouteValues[pagerConfig.CurrentPageKey] = i; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-number\">"
+ getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, i.ToString(), actionName, controllerName, RouteValues) + "</span>" ); } else { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-number "
+ pagerConfig.CssClassPreWord + "-currentnum\">"
+ i.ToString() + "</span>" ); } } //向下… if
(shownEndPageIndex < pagerConfig.TotalPage) { RouteValues[pagerConfig.CurrentPageKey] = shownEndPageIndex + 1; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-number\">"
+ getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "..." , actionName, controllerName, RouteValues) + "</span>" ); } } ////数字导航结束 //下一页 if
(pagerConfig.ShowPrevNextPageLink) { if
(pagerConfig.CurrentPage < pagerConfig.TotalPage) { RouteValues[pagerConfig.CurrentPageKey] = pagerConfig.CurrentPage + 1; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">"
+ getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "下一页" , actionName, controllerName, RouteValues) + "</span>" ); } else { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">下一页</span>" ); } } //尾页 if
(pagerConfig.ShowFirstLastPageLink) { if
(pagerConfig.CurrentPage < pagerConfig.TotalPage) { RouteValues[pagerConfig.CurrentPageKey] = pagerConfig.TotalPage; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">"
+ getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "尾页" , actionName, controllerName, RouteValues) + "</span>" ); } else { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-btn\">尾页</span>" ); } } //右侧记录总数信息 if
(pagerConfig.ShowTotalRecord && pagerConfig.PageInfoPosition == PagerElementPosition.Right) { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-count\">共"
+ pagerConfig.TotalRecord + pagerConfig.RecordUnit + pagerConfig.RecordName + "</span>" ); } //右侧页码信息 if
(pagerConfig.ShowPageInfo && pagerConfig.PageInfoPosition == PagerElementPosition.Right) { sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-info\">第"
+ pagerConfig.CurrentPage + "页/共"
+ pagerConfig.TotalPage + "页</span>" ); } //页码输入框 if
(pagerConfig.ShowGoTo) { RouteValues[pagerConfig.CurrentPageKey] = "--pageRouteValue--" ; sbPager.Append( "<span class=\""
+ pagerConfig.CssClassPreWord + "-goto\">转到第<input class=\""
+ pagerConfig.CssClassPreWord + "-goto-input\" type=\"text\" url=\""
+ Url.Action(actionName, controllerName, RouteValues) + "\" />页" ); if
(pagerConfig.UseAjax) { sbPager.Append( "<input class=\""
+ pagerConfig.CssClassPreWord + "-goto-submit\" type=\"button\" value=\"GO\" onclick=\"$.post( $(this).prev().attr(‘url‘).replace(‘--pageRouteValue--‘,$(this).prev().val()),"
+ pagerConfig.AjaxSuccessFunctionName + ")\" /></span>" ); } else { sbPager.Append( "<input class=\""
+ pagerConfig.CssClassPreWord + "-goto-submit\" type=\"button\" value=\"GO\" onclick=\"window.location = $(this).prev().attr(‘url‘).replace(‘--pageRouteValue--‘,$(this).prev().val());\" /></span>" ); } } //ajax分页回调函数 if
(pagerConfig.UseAjax) { if
(pagerConfig.AutoGenarateAjaxSuccessFunction) { sbPager.Append( "<script type=\"text/javascript\">function "
+ pagerConfig.AjaxSuccessFunctionName + "(data){$(‘#"
+ pagerConfig.AjaxUpdateTargetID + "‘).html(data);}</script>" ); } } sbPager.Append( "</div>" ); return
MvcHtmlString.Create(sbPager.ToString()); } #endregion } } |
调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
public
ActionResult Index() { int
pageSize = 4; int
allCount = db.Movies.Count(); ViewBag.Num = allCount; ViewBag.PageSize = pageSize; int
pageIndex, startIndex, endIndex; //获取开始和结束的记录序号 PagerHelper.GetStartAndEndIndex(allCount, pageSize, out
pageIndex, out
startIndex, out
endIndex); //调用存储过程返回指定序号范围的数据 // return View(db.SelectUserList(startIndex, endIndex)); var
sear = ( from
m in
db.Movies where
m.ID >= startIndex && m.ID <= endIndex select
m).ToList(); return
View(sear); //return View(db.Movies.ToList()); } |
@Html.Pager("Index", "Movies", new { }, new PagerConfig { TotalRecord = ViewBag.Num, PageSize = ViewBag.PageSize })
标签:分页控件 class blog c code java
原文地址:http://www.cnblogs.com/MarchThree/p/3751053.html