Skip to content

Commit 086cd1f

Browse files
Deilanazhukaudev
authored andcommitted
fix(table): fix a bug where source changes and pager doesn't get updated (#329)
1 parent f5496e0 commit 086cd1f

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/ng2-smart-table/components/pager/pager.component.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';
1+
import { Component, Input, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core';
2+
import { Subscription } from 'rxjs/Subscription';
23

34
import { DataSource } from '../../lib/data-source/data-source';
45

@@ -35,7 +36,7 @@ import { DataSource } from '../../lib/data-source/data-source';
3536
</nav>
3637
`,
3738
})
38-
export class PagerComponent implements OnInit {
39+
export class PagerComponent implements OnChanges {
3940

4041
@Input() source: DataSource;
4142

@@ -46,19 +47,25 @@ export class PagerComponent implements OnInit {
4647
protected count: number = 0;
4748
protected perPage: number;
4849

50+
protected dataChangedSub: Subscription;
4951

50-
ngOnInit() {
51-
this.source.onChanged().subscribe((changes) => {
52-
this.page = this.source.getPaging().page;
53-
this.perPage = this.source.getPaging().perPage;
54-
this.count = this.source.count();
55-
if (this.isPageOutOfBounce()) {
56-
this.source.setPage(--this.page);
52+
ngOnChanges(changes: SimpleChanges) {
53+
if (changes.source) {
54+
if (!changes.source.firstChange) {
55+
this.dataChangedSub.unsubscribe();
5756
}
58-
59-
this.processPageChange(changes);
60-
this.initPages();
61-
});
57+
this.dataChangedSub = this.source.onChanged().subscribe((dataChanges) => {
58+
this.page = this.source.getPaging().page;
59+
this.perPage = this.source.getPaging().perPage;
60+
this.count = this.source.count();
61+
if (this.isPageOutOfBounce()) {
62+
this.source.setPage(--this.page);
63+
}
64+
65+
this.processPageChange(dataChanges);
66+
this.initPages();
67+
});
68+
}
6269
}
6370

6471
/**

0 commit comments

Comments
 (0)