forked from xuxiaowei007/FoxOne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPager.cs
158 lines (140 loc) · 4.66 KB
/
Pager.cs
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
using FoxOne.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FoxOne.Core;
using System.ComponentModel;
namespace FoxOne.Controls
{
/// <summary>
/// 翻页组件
/// </summary>
[DisplayName("翻页组件")]
public class Pager : PageControlBase, ITargetId
{
public Pager()
{
PrePageSize = "10,20,50,100";
DisplayIndexCount = 10;
PageSize = 10;
CurrentPageIndex = 1;
CssClass = "data-pager";
}
/// <summary>
/// 每页显示条数
/// </summary>
[DisplayName("每页显示记录")]
public int PageSize { get; set; }
/// <summary>
/// 总记录数
/// </summary>
[DisplayName("总记录数")]
[FormField(Editable = false)]
public int RecordCount { get; set; }
/// <summary>
/// 当前显示页
/// </summary>
[DisplayName("当前显示页")]
[FormField(Editable = false)]
public int CurrentPageIndex { get; set; }
/// <summary>
/// 翻页组件
/// </summary>
[DisplayName("预定义每页显示记录")]
public string PrePageSize { get; set; }
/// <summary>
/// 翻页组件
/// </summary>
[DisplayName("显示页码链接数")]
public int DisplayIndexCount { get; set; }
public void SetTarget(IList<IControl> components)
{
}
public override string Render()
{
Attributes["pageCount"] = PageCount.ToString();
return base.Render();
}
public int PageCount
{
get
{
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize != 0)
{
pageCount++;
}
return pageCount;
}
}
public override string RenderContent()
{
int displayCount = Math.Min(PageCount, DisplayIndexCount);
string pagerItemTemplate = TemplateGenerator.GetPagerItemTemplate();
string pagerSizeTemplate = TemplateGenerator.GetPagerSizeTemplate();
StringBuilder result = new StringBuilder();
result.AppendFormat(pagerItemTemplate, 1, "", "首页");
result.AppendFormat(pagerItemTemplate, "Pre", "", "上一页");
int startIndex = 1, endIndex = displayCount;
int centerValue = (int)Math.Ceiling((double)displayCount / 2);
if (CurrentPageIndex > centerValue && PageCount > displayCount)
{
centerValue = centerValue - 1;
if ((CurrentPageIndex + centerValue) < PageCount)
{
startIndex = CurrentPageIndex - centerValue;
}
else
{
startIndex = PageCount - centerValue * 2;
}
endIndex = Math.Min(PageCount, startIndex + displayCount) - 1;
}
for (int i = startIndex; i <= endIndex; i++)
{
result.AppendFormat(pagerItemTemplate, i, i == CurrentPageIndex ? "class=\"active\"" : "", i);
}
result.AppendFormat(pagerItemTemplate, "Next", "", "下一页");
result.AppendFormat(pagerItemTemplate, PageCount, "", "末页");
string pageSizeSet = string.Empty;
PrePageSize.Split(',').ForEach((a) =>
{
string active = string.Empty;
if (a.Equals(PageSize.ToString()))
{
active = "class=\"active\"";
}
pageSizeSet += pagerSizeTemplate.FormatTo(a, active, a);
});
return TemplateGenerator.GetPagerTemplate().FormatTo(result.ToString(), pageSizeSet);
}
[DisplayName("目标组件ID")]
public string TargetControlId
{
get;
set;
}
}
/// <summary>
/// 翻页控件显示位置
/// </summary>
public enum PagerPosition
{
/// <summary>
/// 表格上方
/// </summary>
[Description("表格上方")]
Top,
/// <summary>
/// 表格下方
/// </summary>
[Description("表格下方")]
Bottom,
/// <summary>
/// 上下方均显示
/// </summary>
[Description("上下方均显示")]
Both
}
}