Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit e994f5d

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
fix pagination should changed on Filter & Sort changed
1 parent b5ea0f9 commit e994f5d

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/app/modules/angular-slickgrid/components/slick-pagination.component.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { castToPromise } from './../services/utilities';
2+
import { FilterService } from '../services/filter.service';
3+
import { SortService } from './../services/sort.service';
24
import { Component, OnInit, Input, AfterViewInit } from '@angular/core';
35
import { Column, GridOption } from './../models';
46

@@ -29,7 +31,7 @@ export class SlickPaginationComponent implements AfterViewInit, OnInit {
2931
paginationCallback: Function;
3032
paginationPageSizes = [25, 75, 100];
3133

32-
constructor() { }
34+
constructor(private filterService: FilterService, private sortService: SortService) { }
3335

3436
ngOnInit() {
3537
}
@@ -39,49 +41,56 @@ export class SlickPaginationComponent implements AfterViewInit, OnInit {
3941
if (!this._gridPaginationOptions || !this._gridPaginationOptions.pagination || (this._gridPaginationOptions.pagination.totalItems !== this.totalItems)) {
4042
this.refreshPagination();
4143
}
44+
45+
// Subscribe to Event Emitter of Filter & Sort changed, go back to page 1 when that happen
46+
this.filterService.onFilterChanged.subscribe((data) => {
47+
this.refreshPagination(true);
48+
});
49+
this.sortService.onSortChanged.subscribe((data) => {
50+
this.refreshPagination(true);
51+
});
4252
}
4353

4454
ceil(number: number) {
4555
return Math.ceil(number);
4656
}
47-
onChangeItemPerPage(event: any) {
48-
const itemsPerPage = event.target.value as number;
49-
this.pageCount = Math.ceil(this.totalItems / itemsPerPage);
50-
this.pageNumber = 1;
51-
this.itemsPerPage = itemsPerPage;
52-
this.onPageChanged(event, this.pageNumber);
53-
}
5457

5558
changeToFirstPage(event: any) {
5659
this.pageNumber = 1;
5760
this.onPageChanged(event, this.pageNumber);
5861
}
62+
5963
changeToLastPage(event: any) {
6064
this.pageNumber = this.pageCount;
6165
this.onPageChanged(event, this.pageNumber);
6266
}
67+
6368
changeToNextPage(event: any) {
6469
if (this.pageNumber < this.pageCount) {
6570
this.pageNumber++;
6671
this.onPageChanged(event, this.pageNumber);
6772
}
6873
}
74+
6975
changeToPreviousPage(event: any) {
7076
if (this.pageNumber > 0) {
7177
this.pageNumber--;
7278
this.onPageChanged(event, this.pageNumber);
7379
}
7480
}
7581

76-
gotoFirstPage() {
82+
onChangeItemPerPage(event: any) {
83+
const itemsPerPage = event.target.value as number;
84+
this.pageCount = Math.ceil(this.totalItems / itemsPerPage);
7785
this.pageNumber = 1;
78-
this.onPageChanged(undefined, this.pageNumber);
86+
this.itemsPerPage = itemsPerPage;
87+
this.onPageChanged(event, this.pageNumber);
7988
}
8089

81-
refreshPagination() {
90+
refreshPagination(isPageNumberReset?: boolean) {
8291
if (this._gridPaginationOptions && this._gridPaginationOptions.pagination) {
8392
// if totalItems changed, we should always go back to the first page and recalculation the From-To indexes
84-
if (this.totalItems !== this._gridPaginationOptions.pagination.totalItems) {
93+
if (isPageNumberReset || this.totalItems !== this._gridPaginationOptions.pagination.totalItems) {
8594
this.pageNumber = 1;
8695
this.recalculateFromToIndexes();
8796
}

src/app/modules/angular-slickgrid/services/filter.service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventEmitter } from '@angular/core';
12
import { castToPromise } from './utilities';
23
import { FilterConditions } from './../filter-conditions';
34
import { FilterTemplates } from './../filter-templates';
@@ -24,6 +25,7 @@ export class FilterService {
2425
_gridOptions: GridOption;
2526
_onFilterChangedOptions: any;
2627
subscriber: SlickEvent;
28+
onFilterChanged = new EventEmitter<string>();
2729

2830
init(grid: any, gridOptions: GridOption, columnDefinitions: Column[]): void {
2931
this._columnDefinitions = columnDefinitions;
@@ -38,6 +40,7 @@ export class FilterService {
3840
*/
3941
attachBackendOnFilter(grid: any, options: GridOption) {
4042
this.subscriber = new Slick.Event();
43+
this.emitFilterChangedBy('remote');
4144
this.subscriber.subscribe(this.attachBackendOnFilterSubscribe);
4245

4346
grid.onHeaderRowCellRendered.subscribe((e: Event, args: any) => {
@@ -117,6 +120,7 @@ export class FilterService {
117120
attachLocalOnFilter(grid: any, options: GridOption, dataView: any) {
118121
this._dataView = dataView;
119122
this.subscriber = new Slick.Event();
123+
this.emitFilterChangedBy('remote');
120124

121125
dataView.setFilterArgs({ columnFilters: this._columnFilters, grid: this._grid });
122126
dataView.setFilter(this.customFilter);
@@ -266,6 +270,15 @@ export class FilterService {
266270
}
267271
}
268272

273+
/**
274+
* A simple function that is attached to the subscriber and emit a change when the sort is called.
275+
* Other services, like Pagination, can then subscribe to it.
276+
* @param {string} sender
277+
*/
278+
emitFilterChangedBy(sender: string) {
279+
this.subscriber.subscribe(() => this.onFilterChanged.emit(`onFilterChanged by ${sender}`));
280+
}
281+
269282
private keepColumnFilters(searchTerm: string, listTerm: any, columnDef: any) {
270283
if (searchTerm) {
271284
this._columnFilters[columnDef.id] = {

src/app/modules/angular-slickgrid/services/sort.service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { EventEmitter } from '@angular/core';
12
import { castToPromise } from './utilities';
23
import { BackendServiceOption, FieldType, GridOption, Sorter } from './../models';
34
import { Sorters } from './../sorters';
45

56
export class SortService {
67
subscriber: any;
8+
onSortChanged = new EventEmitter<string>();
79

810
/**
911
* Attach a backend sort (single/multi) hook to the grid
@@ -12,6 +14,7 @@ export class SortService {
1214
*/
1315
attachBackendOnSort(grid: any, gridOptions: GridOption) {
1416
this.subscriber = grid.onSort;
17+
this.emitSortChangedBy('remote');
1518
this.subscriber.subscribe(this.attachBackendOnSortSubscribe);
1619
}
1720

@@ -48,6 +51,7 @@ export class SortService {
4851
*/
4952
attachLocalOnSort(grid: any, gridOptions: GridOption, dataView: any) {
5053
this.subscriber = grid.onSort;
54+
this.emitSortChangedBy('local');
5155
this.subscriber.subscribe((e: any, args: any) => {
5256
// multiSort and singleSort are not exactly the same, but we want to structure it the same for the (for loop) after
5357
// also to avoid having to rewrite the for loop in the sort, we will make the singleSort an array of 1 object
@@ -97,4 +101,13 @@ export class SortService {
97101
destroy() {
98102
this.subscriber.unsubscribe();
99103
}
104+
105+
/**
106+
* A simple function that is attached to the subscriber and emit a change when the sort is called.
107+
* Other services, like Pagination, can then subscribe to it.
108+
* @param {string} sender
109+
*/
110+
emitSortChangedBy(sender: string) {
111+
this.subscriber.subscribe(() => this.onSortChanged.emit(`onSortChanged by ${sender}`));
112+
}
100113
}

0 commit comments

Comments
 (0)