pagination.js
7.9 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
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
/*分页插件*/
var Pagination = {
showPageCount: 7,
init: function (obj, callback,topFlag) {
this.bindListener(obj, callback,topFlag);
},
createHtml: function (pageIndex, recordCount, pageSize) {
var pageCount = Math.ceil(recordCount / pageSize); //总页数
var showPageCount = this.showPageCount;
var MaxCount = 10000000000;
var HalfPageCount = (showPageCount + 1) / 2;
var html = [];
if (pageCount > MaxCount) {
pageCount = MaxCount;
}
if (pageIndex > pageCount - 1) {
pageIndex = pageCount - 1;
}
html.push("<span class=\"total\">共" + recordCount + "条</span>");
if (pageIndex > 0) {
html.push("<span class=\"previous\"><a href=\"javascript:;\" page= " + (pageIndex - 1) + " data-rec=\"上一页\"></a></span>");
}
else {
html.push("<span class=\"disable previous\"></span>");
}
if (pageCount <= showPageCount) {
for (var i = 0; i < pageCount; i++) {
if (pageIndex == i) {
html.push("<span class=\"current num\">" + (i + 1) + "</span>");
}
else {
html.push("<span class=\"num\"><a href=\"javascript:;\" page=" + i + ">" + (i + 1) + "</a></span>");
}
}
}
else if (pageIndex < HalfPageCount) {
for (var i = 0; i < showPageCount - 1; i++) {
if (pageIndex == i) {
html.push("<span class=\"current num\">" + (i + 1) + "</span>");
}
else {
html.push("<span class=\"num\"><a href=\"javascript:;\" page=" + i + ">" + (i + 1) + "</a></span>");
}
}
html.push("<span class=\"dots\">...</span>");
html.push("<span class=\"num\"><a href=\"javascript:;\" page=" + (pageCount - 1) + ">" + pageCount + "</a></span>");
}
else if (pageIndex >= pageCount - HalfPageCount - 1) {
html.push("<span class=\"num\"><a href=\"javascript:;\" page='0'>" + 1 + "</a></span>");
html.push("<span class=\"dots\">...</span>");
for (var i = pageCount - showPageCount + 1; i < pageCount; i++) {
if (pageIndex == i) {
html.push("<span class=\"current num\">" + (i + 1) + "</span>");
}
else {
html.push("<span class=\"num\"><a href=\"javascript:;\" page=" + i + ">" + (i + 1) + "</a></span>");
}
}
}
else {
html.push("<span class=\"num\"><a href=\"javascript:;\" page=0>" + 1 + "</a></span>");
html.push("<span class=\"dots\">...</span>");
for (var i = pageIndex - HalfPageCount / 2; i <= pageIndex + HalfPageCount / 2; i++) {
if (pageIndex == i) {
html.push("<span class=\"current num\">" + (i + 1) + "</span>");
}
else {
html.push("<span class=\"num\"><a href=\"javascript:;\" page=" + i + ">" + (i + 1) + "</a></span>");
}
}
html.push("<span class=\"dots\">...</span>");
html.push("<span class=\"num\"><a href=\"javascript:;\" page =" + (pageCount - 1) + ">" + pageCount + "</a></span>");
}
if (pageIndex < pageCount - 1) {
html.push("<span class=\"next\"><a href=\"javascript:;\" page=" + (pageIndex + 1) + " data-rec=\"下一页\"></a></span>");
}
else {
html.push("<span class=\"disable next\"></span>");
}
// html.push("<span class=\"total\">每页" + pageSize + "条</span>");
html.push("<span class=\"total total_page\">共" + pageCount + "页</span>");
html.push("<span class=\"page_jump\">到</span><input id='pageInput' class='pageInput' oldpage='' maxlength='" + pageCount + "' type='text' ><span class=\"page_jump\">页</span><button type='button' id='pagebtn' class='pagebtn'>确定</button>");
// html.push("<script>pagination(" + pageCount + ",$('#pageInput'), $('#pagebtn'));</script>");
console.log(pageCount);
console.log(showPageCount);
console.log(HalfPageCount);
return html.join("");
},
bindListener: function (obj, callback,topFlag) {
var topFlag = topFlag || true;
obj.on("click", "a", function () {
if (typeof callback === "function") {
var index = $(this).attr("page");
callback(parseInt(index));
}
if(topFlag){
$(window).scrollTop(0);
}
return false;
});
obj.on("click", "button", function () {
var pageInput = obj.find('input');
if (typeof callback === "function") {
var index = pageInput.val();
if (index == '') {
pageInput.focus();
return false;
// index = 1;
}
callback(parseInt(index) - 1);
}
return false;
});
// var pageButton = $("#pageButton");
obj.on("keyup", "input", function (e) {
switch (e.keyCode) {
case 37:
break;
case 38:
break;
case 39:
break;
case 40:
break;
case 13:
changePage();
break;
case 8:
$(e.target).attr('oldpage', $(e.target).val());
break;
case 46:
$(e.target).attr('oldpage', $(e.target).val());
break;
default :
validPage();
}
return false;
});
//分页
function changePage() {
var pageInput = obj.find('input');
var page = pageInput.val();
var maxPage = parseInt(obj.find(".total_page").html().substring(1));
var pattern = new RegExp("^[1-9]\\d{0," + maxPage.toString().length + "}$");
if (page.trim() == "") {
pageInput.focus();
return;
}
if (pattern.test(page)) {
page = parseInt(page);
maxPage = parseInt(maxPage);
if (page > maxPage || page < 0) {
pageInput.val("").focus();
return;
}
} else {
pageInput.val("").focus();
return;
}
callback(parseInt(page) - 1);
}
//页码校验v2
function validPage() {
var pageInput = obj.find('input');
var totalPageHtml = obj.find(".total_page").html();
var maxPage = parseInt(totalPageHtml.substring(1, totalPageHtml.length - 1));
var page = pageInput.val();
var pattern = /^[0-9]+$/;
var oldpage = pageInput.attr("oldpage") || '';
if (page.trim() == "") {
pageInput.val("");
return;
}
if (!pattern.test(page)) {
pageInput.val(oldpage);
return;
}
var pageInt = parseInt(page);
if (page.substr(0, 1) == '0') {
pageInput.val(pageInt);
}
if (pageInt == 0) {
pageInput.val('');
return;
}
if (pageInt > parseInt(maxPage)) {
pageInput.val(page.substr(0, page.length - 1));
return;
}
pageInput.attr('oldpage', pageInput.val());
}
},
Page: function (obj, pageIndex, recordCount, pageSize) {
obj.empty();
obj.html(this.createHtml(pageIndex, recordCount, pageSize));
}
};