Skip to content

Commit

Permalink
fix another way of breaking the perPage selector
Browse files Browse the repository at this point in the history
fixes #177
  • Loading branch information
uap-universe committed Jan 17, 2025
1 parent a119f4a commit 2b4cdd8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This document lists the changes introduced by this fork.

## Version 3.5.2

* Fix that after removing the unnecessary paging events, the first page was not selected anymore when data was lazy loaded

## Version 3.5.1

* Fix regression: "pages per page" selector broken in version 3.5.0
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular2-smart-table",
"version": "3.5.1",
"version": "3.5.2",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
4 changes: 4 additions & 0 deletions projects/angular2-smart-table/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This document lists the changes introduced by this fork.

## Version 3.5.2

* Fix that after removing the unnecessary paging events, the first page was not selected anymore when data was lazy loaded

## Version 3.5.1

* Fix regression: "pages per page" selector broken in version 3.5.0
Expand Down
2 changes: 1 addition & 1 deletion projects/angular2-smart-table/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular2-smart-table",
"version": "3.5.1",
"version": "3.5.2",
"description": "Angular 2 Smart Table",
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component, Input, OnChanges, SimpleChanges} from '@angular/core';
import {Component, Input, OnChanges, OnDestroy, SimpleChanges} from '@angular/core';
import {Subscription} from 'rxjs';

import {DataSource, DataSourceChangeEvent} from '../../lib/data-source/data-source';
import {DataSource} from '../../lib/data-source/data-source';

@Component({
selector: 'angular2-smart-table-pager',
Expand Down Expand Up @@ -61,7 +61,7 @@ import {DataSource, DataSourceChangeEvent} from '../../lib/data-source/data-sour
`,
standalone: false
})
export class PagerComponent implements OnChanges {
export class PagerComponent implements OnChanges, OnDestroy {

@Input() source!: DataSource;
@Input() perPageSelect!: number[];
Expand All @@ -72,44 +72,41 @@ export class PagerComponent implements OnChanges {
protected count: number = 0;
protected perPage!: number;

protected dataChangedSub!: Subscription;
protected dataChangedSub: Subscription | null = null;

ngOnChanges(changes: SimpleChanges) {
if (changes.source) {
if (!changes.source.firstChange) {
if (this.dataChangedSub !== null) {
this.dataChangedSub.unsubscribe();
}
this.dataChangedSub = this.source.onChanged().subscribe((dataChanges) => {
this.page = this.source.getPaging().page;
this.perPage = this.source.getPaging().perPage;
this.count = this.source.count();
// check, if we are still on a valid page
const lastPage = this.getLast();
if (this.page > lastPage) {
if (dataChanges.action === 'prepend') {
this.source.setPage(1);
} else if (dataChanges.action === 'append') {
this.source.setPage(lastPage);
} else if (this.page > lastPage) {
this.source.setPage(lastPage);
} else if (this.page < 1) {
// for whatever reason...
this.source.setPage(1);
} else {
// do not execute the following function when we needed to adjust the page!
// another event will be emitted and as a reaction we will end up here again
// (in previous versions, this code was executed unnecessarily often)
this.processPageChange(dataChanges);
this.initPages();
}
});
}
}

/**
* We change the page here depending on the action performed against data source
* if a new element was added to the end of the table - then change the page to the last
* if a new element was added to the beginning of the table - then to the first page
* @param changes
*/
processPageChange(changes: DataSourceChangeEvent) {
if (changes.action === 'prepend') {
this.source.setPage(1);
}
if (changes.action === 'append') {
this.source.setPage(this.getLast());
ngOnDestroy() {
if (this.dataChangedSub !== null) {
this.dataChangedSub.unsubscribe();
this.dataChangedSub = null;
}
}

Expand Down Expand Up @@ -140,7 +137,8 @@ export class PagerComponent implements OnChanges {
}

getLast(): number {
return Math.ceil(this.count / this.perPage);
const last = Math.ceil(this.count / this.perPage);
return last === 0 ? 1 : last;
}

initPages() {
Expand Down

0 comments on commit 2b4cdd8

Please sign in to comment.