-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tinypagination.js
226 lines (205 loc) · 7.64 KB
/
jquery.tinypagination.js
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
/**
* www.we3ew.com
*
*$(".paginationsClass").tinypagination({
* changeHandler:function change(page){
* alert(page);
* },
* total: 100,//比设置选项,其他参数可选
* currentPage: 5,
* perPage: 5,
* prevText: '前一页', //默认为“上一页”可不设置
* nextText: '后一页',
* mode:"ajax", //or "nomal"
* pageUrl:"index.php?page=",
* buttonHtmlTag='span';
* pagerHtmlTag='span';
* });
*
* <div class="paginationsClass">第一个</div>
* <div class="paginationsClass">第二个</div>
* <div class="paginationsClass">第三个</div>
*/
(function($) {
function makeUrl(options,currentPage){
return 'href="'+ (options.mode=='ajax'?'#page_': options.pageUrl)+currentPage+'" ';
}
function replacePage(url){
url = url.replace(/\?page=[0-9]{0,11}\&/,'?');
url = url.replace(/\&page=[0-9]{1,11}\&/,'&');
url = url.replace(/page=[0-9]{1,11}\&/,'&');
url = url.replace(/\&page=[0-9]{1,11}/,'');
url = url.replace(/\?page=[0-9]{1,11}/,'');
url = url.replace(/\?page=$/,'');
url = url.replace(/\&page=$/,'');
url = url.replace(/\&\&/,'&');
if(url.indexOf('?')<0)
return url+'?page=';
return url.indexOf('page=')<0?url+'&page=':url;
}
function calculatePagesLinks(options){
var pageNum = calculatePagesNum(options.total, options.currentPage, options.perPage);
var front =new Array();
if(options.frontRange>1){
if(options.currentPage > options.frontRange){
for(var i=0;i<options.frontRange;i++){
front[i]=options.currentPage-options.frontRange+i;
}
}else if(options.currentPage <= options.frontRange && options.currentPage > 1){
for(var i=0;i<(options.currentPage-1);i++){
front[i]=i+1;
}
}
}
var back =new Array();
if(options.backRange>1){
if((pageNum['lastPage']-options.currentPage) >= options.backRange){
var backRangeNum = options.backRange;
if(options.currentPage<=options.frontRange && (pageNum['lastPage']-options.frontRange-options.backRange)>=0){
if(pageNum['lastPage']-options.frontRange-options.backRange>0)
backRangeNum += options.frontRange - options.currentPage + 1;
else
backRangeNum += options.frontRange - options.currentPage;
}
for(var i=0;i<backRangeNum;i++){
back[i]=options.currentPage+i+1;
}
}else if((pageNum['lastPage']-options.currentPage) < options.backRange && pageNum['lastPage']>options.currentPage){
for(var i=0;i<(pageNum['lastPage']-options.currentPage);i++){
back[i]=options.currentPage+i+1;
}
}
}
return [front,back];
}
function calculatePagesNum(total, currentPage, perPage) {
total = parseInt(total);
currentPage = parseInt(currentPage);
perPage = parseInt(perPage);
var tempNum = total % perPage;
var tempPageNum = parseInt(total / perPage);
var allPage = tempNum == 0 ? tempPageNum: (tempPageNum + 1);
var prev = currentPage == 1 ? 1 : (currentPage - 1);
var next = allPage == 1 ? 1 : (allPage > currentPage ? (currentPage + 1) : allPage);
var startItem = (currentPage - 1) * perPage;
var endItem = (currentPage * perPage) > total ? total : (currentPage * perPage);
var result = new Array();
result['total'] = total;
result['homePage'] = total == allPage>0 ? 1 : 0;
result['lastPage'] = allPage;
result['perPage'] = perPage;
result['currentPage'] = currentPage;
result['prev'] = prev;
result['next'] = next;
return result;
}
function creatHrefHtml(className,urlHref,hrefText){
return '<a class="'+className+'" '+ urlHref +' >'+hrefText+'</a> ';
}
function initPaginationHtml(options){
var pageNum = calculatePagesNum(options.total, options.currentPage, options.perPage);
var pageLinks = calculatePagesLinks(options);
var html = '<div class="'+options['containerClass']+'"><span>共 '+pageNum['lastPage']+' 页,'+options.total+' 条</span> ';
//首页
html+= creatHrefHtml(options.startClass, makeUrl(options,1) ,options.startText);
// 前一页
if(pageNum['prev']==1 && pageNum['currentPage']==1){
html+= '<span class="'+options.prevClass+'" >'+options.prevText+'</span> ';
}else{
html+= creatHrefHtml(options.prevClass, makeUrl(options,pageNum['prev']) ,options.prevText);
}
for(var i=0;i<pageLinks[0].length;i++){
html+= creatHrefHtml(options.linksClass, makeUrl(options,pageLinks[0][i]) ,pageLinks[0][i]);
}
// 当前页
html+= '<span class="'+options.currentClass+'" >'+pageNum['currentPage']+'</span> ';
for(var i=0;i<pageLinks[1].length;i++){
html+= creatHrefHtml(options.linksClass, makeUrl(options,pageLinks[1][i]) ,pageLinks[1][i]);
}
// 后一页
if(pageNum['next']==pageNum['lastPage'] && pageNum['next']==pageNum['currentPage']){
html+= '<span class="'+options.nextClass+'" >'+options.nextText+'</span> ';
}else{
html+= creatHrefHtml(options.nextClass, makeUrl(options,pageNum['next']) ,options.nextText);
}
//尾页
html+= creatHrefHtml(options.endClass, makeUrl(options,pageNum['lastPage']) ,options.endText);
html+= '</div>';
return html;
}
$.fn.tinypagination = function(options){
var options = $.extend({
containerClass: 'pagination',
startClass: 'start',
endClass: 'end',
prevClass: 'prev',
nextClass: 'next',
disabledClass: 'disabled',
currentClass: 'current',
linksClass: 'links',
startText: '首页',
prevText: '上一页',
nextText: '下一页',
endText: '尾页',
total: 0,
perPage: 10,
currentPage: 1,
frontRange: 3,
backRange: 3,
changeHandler: function(page){},
mode: "ajax", // normal
pageUrl: window.location.href
},options);
var pageUrl = options.pageUrl;
options.pageUrl = pageUrl.indexOf('?')<0?(pageUrl+'?page='):
(pageUrl.indexOf('page=')<0?(pageUrl+'&page='):replacePage(pageUrl));
var thisList = this;
return this.each(function() {
var $this =$(this);
initPagination(options.currentPage);
$(this).find('.'+ options.containerClass).live('click',function(e){
if(checkClick($(e.target)) && options.mode=='ajax'){
initPagination(getCurrentPage($(e.target)));
}
return true;
});
function checkClick($target){
if($target.attr('tagName')!='A'){
return false;
}
if($target.hasClass(options.prevClass) || $target.hasClass(options.nextClass) || $target.hasClass(options.linksClass)){
return true;
}
return false;
}
function getCurrentPage($target) {
var currentPage;
if ($target.hasClass(options.prevClass)) {
currentPage = countCurrentPage(options.prevClass);
} else if ($target.hasClass(options.nextClass)) {
currentPage = countCurrentPage(options.nextClass);
} else {
currentPage = parseInt($target.text());
}
return currentPage;
}
function initPagination(currentPage){
options.changeHandler.call($(this),currentPage)
if(options.total <= options.perPage){
return false;
}
options.currentPage=currentPage;
var html = initPaginationHtml(options);
thisList.each(function() {
$(this).empty().html(html);
});
return true;
}
function countCurrentPage(type){
options.currentPage = parseInt($this.find('.'+options.currentClass).text());
var pageNum = calculatePagesNum(options.total,options.currentPage,options.perPage);
return pageNum[type];
}
});
};
})(jQuery);