Skip to content

Commit 96cd97c

Browse files
committed
Making sure pagination triggers changed event when items being paginated change
1 parent 2cadb5b commit 96cd97c

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

projects/iqui-ngx/src/lib/data/Pagination/index.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ export class Pagination {
1212
/**
1313
* Changed event triggers when pagination has changed
1414
*/
15-
public changed = new EventEmitter<any>();
15+
public changed = new EventEmitter<void>();
1616

1717
/**
1818
* Holds array of items to be paginated
1919
*/
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+
}
2127

2228
/**
2329
* Holds number of items displayed per page
@@ -31,15 +37,22 @@ export class Pagination {
3137

3238
constructor({ items = [] as any[], pageLength = 10 } = {}) {
3339
// Set properties
34-
this.items = items;
40+
this._items = items;
3541
this._pageLength = pageLength;
3642
}
3743

44+
public updateItems(items = [] as any[]) {
45+
// Update items
46+
this._items = items;
47+
// Trigger change event
48+
this.changed.emit();
49+
}
50+
3851
/**
3952
* Returns array of items on the current page
4053
*/
4154
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);
4356
}
4457

4558
/**
@@ -53,7 +66,7 @@ export class Pagination {
5366
*/
5467
public getCurrentPageLastIndex() {
5568
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;
5770
}
5871
/**
5972
* Gets current page's length
@@ -90,7 +103,7 @@ export class Pagination {
90103
* Checks if previous page exists
91104
*/
92105
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;
94107
}
95108
/**
96109
* Selects the next page as the current page
@@ -110,7 +123,7 @@ export class Pagination {
110123
currentFirstIndex: this.getCurrentPageFirstIndex(),
111124
currentLastIndex: this.getCurrentPageLastIndex(),
112125
pageLength: this.getCurrentPageLength(),
113-
totalLength: this.items.length,
126+
totalLength: this._items.length,
114127
};
115128
}
116129
}

projects/iqui-ngx/src/lib/pipes/Paginate/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import { Pagination } from '../../data';
1212
name: 'iquiPaginate',
1313
})
1414
export class PaginatePipe implements PipeTransform {
15-
public transform(items: any[], startIndex: number, pageLength: number, pagination?: Pagination): any[] {
15+
public transform(items: any[], startIndex: number, pageLength: number, paginationInstanceToBeUpdatedWithNewItems?: Pagination): any[] {
1616
// Update data being paginated (if pagination provided)
17-
if (pagination) {
18-
pagination.items = items;
17+
if (paginationInstanceToBeUpdatedWithNewItems) {
18+
paginationInstanceToBeUpdatedWithNewItems.updateItems(items);
1919
}
2020
// Return current page range of items
2121
return (items || []).slice(startIndex, startIndex + pageLength);

0 commit comments

Comments
 (0)