@@ -12,12 +12,18 @@ export class Pagination {
12
12
/**
13
13
* Changed event triggers when pagination has changed
14
14
*/
15
- public changed = new EventEmitter < any > ( ) ;
15
+ public changed = new EventEmitter < void > ( ) ;
16
16
17
17
/**
18
18
* Holds array of items to be paginated
19
19
*/
20
- public items : any [ ] ;
20
+ private _items : any [ ] ;
21
+ /**
22
+ * Gets array of items to be paginated
23
+ */
24
+ public get items ( ) : any [ ] {
25
+ return this . _items ;
26
+ }
21
27
22
28
/**
23
29
* Holds number of items displayed per page
@@ -31,15 +37,22 @@ export class Pagination {
31
37
32
38
constructor ( { items = [ ] as any [ ] , pageLength = 10 } = { } ) {
33
39
// Set properties
34
- this . items = items ;
40
+ this . _items = items ;
35
41
this . _pageLength = pageLength ;
36
42
}
37
43
44
+ public updateItems ( items = [ ] as any [ ] ) {
45
+ // Update items
46
+ this . _items = items ;
47
+ // Trigger change event
48
+ this . changed . emit ( ) ;
49
+ }
50
+
38
51
/**
39
52
* Returns array of items on the current page
40
53
*/
41
54
public getCurrentPageRange ( ) {
42
- return this . items . slice ( this . _currentPage * this . _pageLength , ( this . _currentPage + 1 ) * this . _pageLength ) ;
55
+ return this . _items . slice ( this . _currentPage * this . _pageLength , ( this . _currentPage + 1 ) * this . _pageLength ) ;
43
56
}
44
57
45
58
/**
@@ -53,7 +66,7 @@ export class Pagination {
53
66
*/
54
67
public getCurrentPageLastIndex ( ) {
55
68
const lastIndex = ( this . _currentPage + 1 ) * this . _pageLength - 1 ;
56
- return lastIndex <= this . items . length - 1 ? lastIndex : this . items . length - 1 ;
69
+ return lastIndex <= this . _items . length - 1 ? lastIndex : this . _items . length - 1 ;
57
70
}
58
71
/**
59
72
* Gets current page's length
@@ -90,7 +103,7 @@ export class Pagination {
90
103
* Checks if previous page exists
91
104
*/
92
105
public checkNextPage ( ) {
93
- return this . _currentPage < Math . ceil ( this . items . length / this . _pageLength ) - 1 ;
106
+ return this . _currentPage < Math . ceil ( this . _items . length / this . _pageLength ) - 1 ;
94
107
}
95
108
/**
96
109
* Selects the next page as the current page
@@ -110,7 +123,7 @@ export class Pagination {
110
123
currentFirstIndex : this . getCurrentPageFirstIndex ( ) ,
111
124
currentLastIndex : this . getCurrentPageLastIndex ( ) ,
112
125
pageLength : this . getCurrentPageLength ( ) ,
113
- totalLength : this . items . length ,
126
+ totalLength : this . _items . length ,
114
127
} ;
115
128
}
116
129
}
0 commit comments