-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathPager.cs
More file actions
121 lines (103 loc) · 3.67 KB
/
Pager.cs
File metadata and controls
121 lines (103 loc) · 3.67 KB
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
using System;
namespace BlogEngine.Core
{
/// <summary>
/// Pager for data grids
/// </summary>
public static class Pager
{
#region Properties
/// <summary>
/// First page
/// </summary>
static int First { get; set; }
/// <summary>
/// Previous page
/// </summary>
static int Previous { get; set; }
/// <summary>
/// Show items on the page from
/// </summary>
static int From { get; set; }
/// <summary>
/// Show items from the page up to
/// </summary>
static int To { get; set; }
/// <summary>
/// Next page in the list
/// </summary>
static int Next { get; set; }
/// <summary>
/// Last page in the list
/// </summary>
static int Last { get; set; }
/// <summary>
/// Current page
/// </summary>
static int CurrentPage { get; set; }
/// <summary>
/// Records total
/// </summary>
static int Total { get; set; }
#endregion
/// <summary>
/// Pager constructor
/// </summary>
/// <param name="page">Page #</param>
/// <param name="pageSize">Page size (number of items per page)</param>
/// <param name="listCount">Number of items in the list</param>
public static void Reset(int page, int pageSize, int listCount)
{
if (page < 1) page = 1;
Total = listCount;
var pgs = Convert.ToDecimal(listCount) / Convert.ToDecimal(pageSize);
var p = pgs - (int)pgs;
Last = p > 0 ? (int)pgs + 1 : (int)pgs;
if (page > Last) page = 1;
CurrentPage = page;
From = ((page * pageSize) - (pageSize - 1));
To = (page * pageSize);
// adjust for the Last (or single) page
if (listCount < To) To = listCount;
// when Last item on the Last page deleted
// this will reset "from" counter
if (From > To) From = From - pageSize;
if (page > 1)
{
Previous = page - 1;
First = 1;
}
if (page < Last) Next = page + 1;
if (page == Last) Last = 0;
}
/// <summary>
/// Renders pager HMTML
/// </summary>
/// <param name="page">Current page</param>
/// <param name="callback">Callback for JavaScript</param>
/// <returns>HTML markup</returns>
public static string Render(string callback = "false")
{
var prvLnk = string.Empty;
var nxtLnk = string.Empty;
var firstLnk = string.Empty;
var lastLnk = string.Empty;
if (string.IsNullOrEmpty(callback))
callback = "false";
var linkFormat = "<a href=\"#\" id=\"{0}\" onclick=\"return " + callback + ";\" class=\"{0}\">{1}</a>";
var pageLink = string.Format("<span>Showing {0} - {1} of {2}</span>", From, To, Total);
if (CurrentPage > 1)
{
prvLnk = string.Format(linkFormat, "prevLink", Previous);
firstLnk = string.Format(linkFormat, "firstLink", First);
}
if (CurrentPage < Last)
{
nxtLnk = string.Format(linkFormat, "nextLink", Next);
lastLnk = string.Format(linkFormat, "lastLink", Last);
}
var currpage = "<span id=\"current-page\" style=\"display:none\">" + CurrentPage.ToString() + "</span>";
return firstLnk + prvLnk + pageLink + nxtLnk + lastLnk + currpage;
}
}
}