简单需求
实现分页。而且当分页数量较大时,最多只显示自定义的页数。(比如只显示10页)
Paging
BaseDto.cs
[c#]
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/// <summary>
/// base request
/// </summary>
[Serializable]
public abstract class BaseDto
{
//tostring();
public static string ToString(ArrayList arrayList)
{
int count = arrayList.Count;
if (count == 0)
{
return "[]";
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("[");
foreach (object obj in arrayList)
{
stringBuilder.Append(obj).Append(",").Append(" ");
}
//Remove the last , append;
stringBuilder.Remove(stringBuilder.Length-2, 2);
stringBuilder.Append("]");
return stringBuilder.ToString();
}
}
BaseRequest.cs
[c#]
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/// <summary>
/// Pageable request.
/// </summary>
public class PageableRequest : BaseDto
{
/// <summary>
/// The page number.
/// </summary>
private int pageNum;
/// <summary>
/// The size of the page.
/// </summary>
private int pageSize = 10;
/// <summary>
/// The order by.
/// </summary>
private String orderBy;
public int PageNum
{
get
{
return pageNum;
}
set
{
pageNum = value;
}
}
public int PageSize
{
get
{
return pageSize;
}
set
{
pageSize = value;
}
}
public string OrderBy
{
get
{
return orderBy;
}
set
{
orderBy = value;
}
}
}
BaseResponse.cs
[c#]
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
38public class BaseResponse : BaseDto
{
/// <summary>
/// The resp code.
/// </summary>
private String respCode;
/// <summary>
/// The message.
/// </summary>
private String msg;
public string RespCode
{
get
{
return respCode;
}
set
{
respCode = value;
}
}
public string Msg
{
get
{
return msg;
}
set
{
msg = value;
}
}
}
PageableResponse.cs
[c#]
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322/**
* <p/>
* 新增分页的多项属性,主要参考:http://bbs.csdn.net/topics/360010907
*
* @author liuzh/abel533/isea533
* @version 3.3.0
* @since 3.2.2
* 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
*/
public class PageableResponse<T> : BaseResponse
{
#region base fields
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private bool isFirstPage = false;
//是否为最后一页
private bool isLastPage = false;
//是否有前一页
private bool hasPreviousPage = false;
//是否有下一页
private bool hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
#endregion
#region
public int PageNum
{
get
{
return pageNum;
}
}
public int PageSize
{
get
{
return pageSize;
}
}
public int Size
{
get
{
return size;
}
}
public int StartRow
{
get
{
return startRow;
}
}
public int EndRow
{
get
{
return endRow;
}
}
public long Total
{
get
{
return total;
}
}
public int Pages
{
get
{
return pages;
}
}
public List<T> List
{
get
{
return list;
}
}
public int PrePage
{
get
{
return prePage;
}
}
public int NextPage
{
get
{
return nextPage;
}
}
public bool IsFirstPage
{
get
{
return isFirstPage;
}
}
public bool IsLastPage
{
get
{
return isLastPage;
}
}
public bool HasPreviousPage
{
get
{
return hasPreviousPage;
}
}
public bool HasNextPage
{
get
{
return hasNextPage;
}
}
public int NavigatePages
{
get
{
return navigatePages;
}
}
public int[] NavigatepageNums
{
get
{
return navigatepageNums;
}
}
public int NavigateFirstPage
{
get
{
return navigateFirstPage;
}
}
public int NavigateLastPage
{
get
{
return navigateLastPage;
}
}
#endregion
PageableResponse(List<T> list, int navigatePages)
{
int count = list.Count;
if (list is ICollection)
{
this.pageNum = 1;
this.pageSize = count;
this.pages = 1;
this.list = list;
this.size = count;
this.total = count;
this.startRow = 0;
this.endRow = count > 0 ? count - 1 : 0;
}
if (list is ICollection)
{
this.navigatePages = navigatePages;
//计算导航页
calcNavigatepageNums();
//计算前后页,第一页,最后一页
calcPage();
//判断页面边界
judgePageBoudary();
}
}
public PageableResponse(List<T> list) : this(list, 8)
{
}
/**
* 计算导航页
*/
private void calcNavigatepageNums()
{
//当总页数小于或等于导航页码数时
if (pages <= navigatePages)
{
navigatepageNums = new int[pages];
for (int i = 0; i < pages; i++)
{
navigatepageNums[i] = i + 1;
}
}
else { //当总页数大于导航页码数时
navigatepageNums = new int[navigatePages];
int startNum = pageNum - navigatePages / 2;
int endNum = pageNum + navigatePages / 2;
if (startNum < 1)
{
startNum = 1;
//(最前navigatePages页
for (int i = 0; i < navigatePages; i++)
{
navigatepageNums[i] = startNum++;
}
}
else if (endNum > pages)
{
endNum = pages;
//最后navigatePages页
for (int i = navigatePages - 1; i >= 0; i--)
{
navigatepageNums[i] = endNum--;
}
}
else {
//所有中间页
for (int i = 0; i < navigatePages; i++)
{
navigatepageNums[i] = startNum++;
}
}
}
}
/**
* 计算前后页,第一页,最后一页
*/
private void calcPage()
{
if (navigatepageNums != null && navigatepageNums.Length > 0)
{
navigateFirstPage = navigatepageNums[0];
navigateLastPage = navigatepageNums[navigatepageNums.Length - 1];
if (pageNum > 1)
{
prePage = pageNum - 1;
}
if (pageNum < pages)
{
nextPage = pageNum + 1;
}
}
}
/**
* 判定页面边界
*/
private void judgePageBoudary()
{
isFirstPage = pageNum == 1;
isLastPage = pageNum == pages;
hasPreviousPage = pageNum > 1;
hasNextPage = pageNum < pages;
}
public override string ToString()
{
return string.Format("[PageableResponse: pageNum={0}, pageSize={1}, size={2}, startRow={3}, endRow={4}, total={5}, pages={6}, list={7}, prePage={8}, nextPage={9}, isFirstPage={10}, isLastPage={11}, hasPreviousPage={12}, hasNextPage={13}, navigatePages={14}, navigatepageNums={15}, navigateFirstPage={16}, navigateLastPage={17}, PageNum={18}, PageSize={19}, Size={20}, StartRow={21}, EndRow={22}, Total={23}, Pages={24}, List={25}, PrePage={26}, NextPage={27}, IsFirstPage={28}, IsLastPage={29}, HasPreviousPage={30}, HasNextPage={31}, NavigatePages={32}, NavigatepageNums={33}, NavigateFirstPage={34}, NavigateLastPage={35}]", pageNum, pageSize, size, startRow, endRow, total, pages, list, prePage, nextPage, isFirstPage, isLastPage, hasPreviousPage, hasNextPage, navigatePages, navigatepageNums, navigateFirstPage, navigateLastPage, PageNum, PageSize, Size, StartRow, EndRow, Total, Pages, List, PrePage, NextPage, IsFirstPage, IsLastPage, HasPreviousPage, HasNextPage, NavigatePages, NavigatepageNums, NavigateFirstPage, NavigateLastPage);
}
}
简单测试
- ExeTest.cs
[c#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14class ExeTest
{
static void Main(string[] args)
{
Console.Write("");
System.Collections.Generic.List<string> stringList = new System.Collections.Generic.List<string>();
stringList.Add("One");
stringList.Add("two");
PageableResponse<string> response = new PageableResponse<string>(stringList);
Console.WriteLine("response : {0}", response);
}
}
- result
[plaintext]
1response : [PageableResponse: pageNum=1, pageSize=2, size=2, startRow=0, endRow=1, total=2, pages=1, list=System.Collections.Generic.List`1[System.String], prePage=0, nextPage=0, isFirstPage=True, isLastPage=True, hasPreviousPage=False, hasNextPage=False, navigatePages=8, navigatepageNums=System.Int32[], navigateFirstPage=1, navigateLastPage=1, PageNum=1, PageSize=2, Size=2, StartRow=0, EndRow=1, Total=2, Pages=1, List=System.Collections.Generic.List`1[System.String], PrePage=0, NextPage=0, IsFirstPage=True, IsLastPage=True, HasPreviousPage=False, HasNextPage=False, NavigatePages=8, NavigatepageNums=System.Int32[], NavigateFirstPage=1, NavigateLastPage=1]
备注:
1、在C#中Collection和Array无法直接打印出内容。需要特殊处理。为此将在 BaseDto.cs
中添加反射 ToString()
,并处理此特殊情况。
2、PageSize 在查询记录如果少于分页大小时是错误的。实际应用中,PageSize是前端传过来的。不需要后台再次反馈回去。