pagination.js
1.28 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
let _ = require('lodash');
class Pagination {
constructor(url, page, totalRecords, pageSize){
let options = {};
let len = arguments.length;
let args = [].slice.call(arguments);
let _pageSize = 0;
if(args.length && _.isPlainObject(args[len - 1])) {
options = args.pop();
}
this.baseUrl = args.shift() || options.baseUrl;
this.page = args.shift() || 1;
this.totalRecords = args.shift() || 0;
_pageSize = args.shift() || options.pageSize;
this.pageSize = _pageSize || 10;
this.totalPages = options.totalPages || 0;
this.theme = options.theme || 'full';
this.pageVar = options.pageVar || 'page';
this.pageSizeVar = options.pageSizeVar || 'pageSize';
if(!this.totalPages && this.totalRecords) {
this.totalPages = Math.ceil(this.totalRecords / this.pageSize);
}
this.base = this.baseUrl + (this.baseUrl.indexOf('?') < 0 ? '?' : '&') +
(_pageSize ? (this.pageSizeVar + '=' + this.pageSize + '&') : '') +
this.pageVar + '=';
}
hasNextPage() {
return this.page < this.totalPages;
}
hasPrevPage() {
return this.page > 1;
}
getUrl(page) {
return this.base + page || 1;
}
}