pagination.js
5.61 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
/**
* 分页组件
* @author: djh<jinhu.dong@yoho.cn>
* @date: 2016/6/29
*/
'use strict';
// render pagination ellipse indicator
function _renderEllipse(templateStr) {
return templateStr + '<li><a>...</a></li>';
}
// render last page link
function _renderLastPage(templateStr, pageCount, queryParams) {
return templateStr + '<li><a href="?page=' + pageCount + queryParams + '">' + pageCount + '</a></li>';
}
/*
* Pagination Component
* @param { Object } pagination pagination constructor
* --- pagination ---
* @param { Integer } limit: per page records number
* @param { String } leftText: prev button text, default is iconfont
* @param { String } rightText: next button text, default is iconfont
* @param { String } paginationClass: pagination ul class, default is 'blk-pagination'
*/
exports.createPagination = function(pagination, options) {
// default options
var limit = 9, // display links number
n, // page number ?page=n
queryParams = '', // paginate with query parameter
page = parseInt(pagination.page, 10), // current page number
leftText = '<i class="iconfont"></i>', // prev
rightText = '<i class="iconfont"></i>', // next
paginationClass = 'blk-pagination'; // pagination <ul> default class
var pageCount,
key, // query params key
lastCharacterOfQueryParams;
var i, // iterator for each refresh page
leftCount, // left pagination link counts for current position
rightCount, // right pagination link counts for current position
start; // left first link number
var template;
if (!pagination) {
return '';
}
// custome options
if (options.hash.limit) {
limit = +options.hash.limit;
}
if (options.hash.leftText) {
leftText = options.hash.leftText;
}
if (options.hash.rightText) {
rightText = options.hash.rightText;
}
if (options.hash.paginationClass) {
paginationClass = options.hash.paginationClass;
}
pageCount = Math.ceil(pagination.totalRows / pagination.limit);
// query params
if (pagination.queryParams) {
queryParams = '&';
for (key in pagination.queryParams) {
if (pagination.queryParams.hasOwnProperty(key) && key !== 'page') {
queryParams += key + '=' + pagination.queryParams[key] + '&';
}
}
lastCharacterOfQueryParams = queryParams.substring(queryParams.length, -1);
if (lastCharacterOfQueryParams === '&') {
// trim off last & character
queryParams = queryParams.substring(0, queryParams.length - 1);
}
}
template = '<ul class="' + paginationClass + '">';
// ========= Previous Button ===============
if (page - 1) {
n = page - 1;
template = template + '<li><a href="?page=' + n + queryParams + '">' + leftText + '</a></li>';
}
// ========= Page Numbers Middle ===============
// current is active, and make left page link count equal right link count
i = 0;
leftCount = Math.ceil(limit / 2) - 1;
rightCount = limit - leftCount - 1;
if (page + rightCount > pageCount) {
leftCount = limit - (pageCount - page) - 1;
}
if (page - leftCount < 1) {
leftCount = page - 1;
}
start = page - leftCount;
while (i < limit && i < pageCount) {
// start is every link
n = start;
if (start === page) {
template = template + '<li class="active"><a href="?page=' + n + queryParams + '">' + n + '</a></li>';
} else {
// generate left style
if (leftCount >= 4) {
if (i === 0) {
// first page
template = template + '<li><a href="?page=1' + queryParams + '">1</a></li>';
} else if (i === 1 || (i === 7 && start <= pageCount - 2)) {
// left and right ...
template = _renderEllipse(template);
} else if (i === 8 && start <= pageCount - 1) {
template = _renderLastPage(template, pageCount, queryParams);
} else {
// other links is normal
template = template +
'<li><a href="?page=' + n + queryParams + '">' +
n +
'</a></li>';
}
} else {
if (i === 7 && start <= pageCount - 2) {
// right ...
template = _renderEllipse(template);
} else if (i === 8 && start <= pageCount - 1) {
template = _renderLastPage(template, pageCount, queryParams);
} else {
template = template +
'<li><a href="?page=' + n + queryParams + '">' +
n +
'</a></li>';
}
}
}
start++;
i++;
}
// ========= Next page ===============
if (pageCount - page) {
n = page + 1;
template = template + '<li><a href="?page=' + n + queryParams + '">' + rightText + '</a></li>';
}
template = template + '</ul>';
// html template
return template;
};