-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-paginator.js
55 lines (52 loc) · 1.8 KB
/
simple-paginator.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
import { ReactiveVar } from 'meteor/reactive-var'
// Write your package code here!
// Variables exported by this module can be imported by other packages and
// applications. See simple-paginator-tests.js for an example of importing.
export const paginator = {
arr: new ReactiveVar([]),
startIndex: new ReactiveVar(0),
endIndex: new ReactiveVar(0),
pageSize: new ReactiveVar(3),
pageNumber: new ReactiveVar(1),
totalPages: new ReactiveVar(0),
setArray(arr) {
this.arr.set(arr);
return this;
},
getTotalPages() {
const totalPages = this.arr.get().length / this.pageSize.get();
let pagesArr = [];
for (let i = 0; i < totalPages; i++) {
pagesArr.push(i + 1);
}
this.totalPages.set(pagesArr);
return this.totalPages.get();
},
getPage() {
this.endIndex.set(this.pageNumber.get() * this.pageSize.get());
this.startIndex.set((this.pageNumber.get() - 1) * this.pageSize.get());
const items = this.arr.get().slice(this.startIndex.get(), this.endIndex.get());
return items;
},
currPageNumber() {
return this.pageNumber.get();
},
incrementPage() {
let pageNumber = this.pageNumber.get();
(pageNumber + 1 > this.totalPages.get().length) ? '' : this.pageNumber.set(pageNumber + 1);
return this;
},
decrementPage() {
const pageNumber = this.pageNumber.get();
(pageNumber === 1) ? '' : this.pageNumber.set(pageNumber - 1);
return this
},
setPageNumber(pageNumber) {
(pageNumber < 1) ? this.pageNumber.set(1) : this.pageNumber.set(pageNumber);
return this
},
setPageSize(pageSize) {
(pageSize < 1) ? this.pageSize.set(1) : this.pageSize.set(pageSize);
return this;
}
};