pager.js
6.27 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
/**
* guang
* @author: lcy<chuanyang.liu@yoho.cn>
* @date: 2016/09/01
*/
'use strict';
var $ = require('yoho-jquery');
var ALL_TYPES = {
stand: 'p-page-n',
mini: 'p-n',
full: 'f-p-page-n-l-info-input-gobtn',
fullellipsis: 'f-p-pe-n-l-info-input-gobtn',
ellipsis: 'p-pe-n'
};
module.exports = function(opts) {
var options = opts || {};
var baseUrl = options.baseUrl || '',
page = (options.page || 1) * 1,
showNum = (options.showNum || 7) * 1,
pageSize = options.pageSize || 20,
totalPages = options.totalPages,
totalRecords = options.totalRecords,
theme = options.theme || 'pager',
currentClass = options.currentClass || 'cur',
// min full stand ellipsis or: f-首页, p-上一页, page-页码,n-下一页,l-最后一页,info-displayMsg,inout输入框,gobtn-goto Btn
type = options.type || 'stand',
pageVar = options.pageVar || 'page',
pageSizeVar = options.pageSizeVar || 'pageSize';
// 清除原来page(page=1&) 重新定义page
var clearPageReg = new RegExp(pageVar + '=[^&]*(&|$)'),
clearSizeReg = new RegExp(pageSizeVar + '=[^&]*(&|$)'),
base = baseUrl.replace(clearPageReg, '');
if (options.pageSize) {
base = base.replace(clearSizeReg, '');
}
base += (base.indexOf('?') < 0 ? '?' : (/(\?|&)$/.test(base) ? '' : '&')) + // eslint-disable-line
(options.pageSize ? (pageSizeVar + '=' + pageSize + '&') : '') +
pageVar + '=';
function getPageNums(ntype) {
/** 分页展示页码个数begin 规则:展示最靠近当前页的指定个数 **/
var num = showNum;
var pageShowMax = num % 2 === 0 ? page - 1 : page;
var pageShowMin = page;
var pageNums = [];
var i, n;
if (ntype === 'e') {
num = num - 2;
num = num > 2 ? num : 2;
}
for (i = 0; i < Math.floor(num / 2); i++) {
pageShowMax++;
pageShowMin--;
if (pageShowMax > totalPages) {
pageShowMax = totalPages;
if (pageShowMin > 1) {
pageShowMin--;
}
}
if (pageShowMin < 1) {
pageShowMin = 1;
if (pageShowMax < totalPages) {
pageShowMax++;
}
}
}
for (n = pageShowMin; n <= pageShowMax; n++) {
pageNums.push(n);
}
if (ntype === 'e') {
if (pageShowMin > 3) {
pageNums.unshift(1, '.');
} else if (pageShowMin === 2) {
pageNums.unshift(1);
} else if (pageShowMin === 3) {
pageNums.unshift(1, 2);
}
if (pageShowMax < totalPages - 2) {
pageNums.push('.', totalPages);
} else {
for (var x = pageShowMax + 1; x <= totalPages; x++) { // eslint-disable-line
pageNums.push(x);
}
}
}
return pageNums;
}
function renderItem(arr) {
/** 分页展示页码个数end **/
var ret = '';
if ($.isArray(arr) && arr.length) {
$.each(arr, function(i, val) {
if (val === '.') {
ret += '<a>...</a>';
} else {
ret += '<a href="javascript:void(0)" data-page="' + base + val + '"' +
(page === val ? 'class="' + currentClass + '"' : '') +
' title="第' + val + '页">' + val + '</a>';
}
});
}
return ret;
}
function createStandItems() {
return renderItem(getPageNums());
}
function createEllipsisItems() {
return renderItem(getPageNums('e'));
}
if (!totalPages) {
if (!totalRecords || !pageSize) {
return '';
} else {
totalPages = Math.ceil(totalRecords / pageSize);
}
}
var items = ALL_TYPES[type] ? ALL_TYPES[type] : type; // eslint-disable-line
items = $.isArray(items) ? items : (items || ALL_TYPES.stand).split('-');
// 配置中如果配置了多次 page/pe 则将忽略,只第一次有效
var hasPage = false; // eslint-disable-line
var ret = '<div class="pager' + ' ' + theme + '">'; // eslint-disable-line
$.each(items, function(i, val) {
switch (val) {
case 'f' :
if (page > 1) {
ret += '<a href="javascript:void(0)" title="首页" data-page="' + base + '1">首页</a>';
}
break;
case 'p' :
if (page > 1) {
ret += '<a href="javascript:void(0)" data-page="' + base + (page - 1) +
'" title="上一页"><span class="iconfont"></span>上一页</a>';
}
break;
case 'n' :
if (page < totalPages) {
ret += '<a href="javascript:void(0)" data-page="' + base + (page + 1) +
'" title="下一页">下一页<span class="iconfont"></span></a>';
}
break;
case 'l' :
if (page < totalPages) {
ret += '<a href="javascript:void(0)" data-page="' + base + totalPages + '" title="尾页">尾页</a>';
}
break;
case 'info' :
ret += '共{totalRecords}条/{totalPages}页';
break;
case 'input' :
// input
// ret += '<span class="{cls}"><input value="{value}" type="text"></span>';
break;
case 'gobtn' :
// goto btn
break;
case 'page' :
if (!hasPage) {
ret += createStandItems();
}
hasPage = true;
break;
case 'pe' :
if (!hasPage) {
ret += createEllipsisItems();
}
hasPage = true;
break;
default:
break;
}
});
if (options.fn) {
ret += options.fn(options.context);
}
ret += '</div>';
return ret;
};