Skip to content

Commit 5bd562f

Browse files
ddinchevazdrawkukdinev
authored
Add totalRecords input property (#7342)
* feat(IgxGrid): add input property total records to the grid #7215 * fix(IgxGrid): add paging mode property to the grid #7215 * feat(IgxTreeGrid): add pagingMode property to tree and hierarchical grid #7215 Co-authored-by: Zdravko Kolev <[email protected]> Co-authored-by: Konstantin Dinev <[email protected]>
1 parent 96f1d39 commit 5bd562f

File tree

9 files changed

+77
-19
lines changed

9 files changed

+77
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ All notable changes for each version of this project will be documented in this
129129
```typescript
130130
public pinningConfiguration: IPinningConfig = { columns: ColumnPinningPosition.End };
131131
```
132+
- Added new properties for paging:
133+
- `totalRecords` set to alter the pages count based on total remote records. Keep in mind that If you are using paging and all the data is passed to the grid, the value of totalRecords property will be set by default to the length of the provided data source. If totalRecords is set, it will take precedent over the default length based on the data source.
134+
- `pagingMode` - accepts `GridPagingMode` enumeration. If the paging mode is set to remote the grid will not paginate the passed data source, if the paging mode is set to local (which is the default value) the grid will paginate the data source based on the page, perPage and totalRecords values.
132135
- Added functionality for column selection.
133136
- `columnSelection` property has been added. It accepts GridSelection mode enumeration. Grid selection mode could be none, single or multiple.
134137
- `selected` property has been added to the IgxColumnComponent; Allows you to set whether the column is selected.

projects/igniteui-angular/src/lib/data-operations/data-util.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ export class DataUtil {
7272
return grouping.groupBy(data, state, grid, groupsRecords, fullResult);
7373
}
7474

75-
public static page<T>(data: T[], state: IPagingState): T[] {
75+
public static page<T>(data: T[], state: IPagingState, dataLength?: number): T[] {
7676
if (!state) {
7777
return data;
7878
}
79-
const len = data.length;
79+
const len = dataLength !== undefined ? dataLength : data.length;
8080
const index = state.index;
8181
const res = [];
82-
const recordsPerPage = state.recordsPerPage;
82+
const recordsPerPage = dataLength !== undefined && state.recordsPerPage > dataLength ? dataLength : state.recordsPerPage;
8383
state.metadata = {
8484
countPages: 0,
85-
countRecords: data.length,
85+
countRecords: len,
8686
error: PagingError.None
8787
};
8888
if (index < 0 || isNaN(index)) {

projects/igniteui-angular/src/lib/grids/common/enums.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ export enum RowPinningPosition {
4242
Top,
4343
Bottom
4444
}
45+
46+
export enum GridPagingMode {
47+
local,
48+
remote
49+
}

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ import {
113113
GridSummaryCalculationMode,
114114
FilterMode,
115115
ColumnPinningPosition,
116-
RowPinningPosition
116+
RowPinningPosition,
117+
GridPagingMode
117118
} from './common/enums';
118119
import {
119120
IGridCellEventArgs,
@@ -409,6 +410,17 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
409410
this._locale = value;
410411
}
411412

413+
@Input()
414+
get pagingMode() {
415+
return this._pagingMode;
416+
}
417+
418+
set pagingMode(val: GridPagingMode) {
419+
this._pagingMode = val;
420+
this._pipeTrigger++;
421+
this.notifyChanges(true);
422+
}
423+
412424
/**
413425
* Gets/Sets whether the paging feature is enabled.
414426
* @remarks
@@ -2489,6 +2501,14 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
24892501
* @hidden
24902502
*/
24912503
protected _paging = false;
2504+
/**
2505+
* @hidden
2506+
*/
2507+
protected _pagingMode = GridPagingMode.local;
2508+
/**
2509+
* @hidden @internal
2510+
*/
2511+
public _totalRecords = -1;
24922512
/**
24932513
* @hidden
24942514
*/
@@ -3614,7 +3634,7 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
36143634
if (this.pagingState) {
36153635
return this.pagingState.metadata.countPages;
36163636
}
3617-
return -1;
3637+
return this._totalRecords >= 0 ? Math.ceil(this._totalRecords / this.perPage) : -1;
36183638
}
36193639

36203640
/**
@@ -3663,9 +3683,16 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
36633683
* const totalRecords = this.grid.totalRecords;
36643684
* ```
36653685
*/
3686+
@Input()
36663687
get totalRecords(): number {
3667-
if (this.pagingState) {
3668-
return this.pagingState.metadata.countRecords;
3688+
return this._totalRecords >= 0 ? this._totalRecords : this.pagingState?.metadata.countRecords;
3689+
}
3690+
3691+
set totalRecords(total: number) {
3692+
if (total >= 0) {
3693+
this._totalRecords = total;
3694+
this._pipeTrigger++;
3695+
this.notifyChanges();
36693696
}
36703697
}
36713698

projects/igniteui-angular/src/lib/grids/grid/grid.pagination.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ describe('IgxGrid - Grid Paging #grid', () => {
141141
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
142142
});
143143

144+
it('should be able to set totalRecords', () => {
145+
grid.perPage = 5;
146+
fix.detectChanges();
147+
148+
expect(grid.paging).toBeTruthy();
149+
expect(grid.perPage).toEqual(5, 'Invalid page size');
150+
expect(grid.totalRecords).toBe(10);
151+
verifyGridPager(fix, 5, '1', '1\xA0of\xA02', []);
152+
153+
grid.totalRecords = 4;
154+
fix.detectChanges();
155+
156+
expect(grid.perPage).toEqual(5, 'Invalid page size');
157+
expect(grid.totalRecords).toBe(4);
158+
verifyGridPager(fix, 4, '1', '1\xA0of\xA01', []);
159+
});
160+
161+
144162
it('change paging settings UI', () => {
145163

146164
expect(grid.paging).toBeTruthy();

projects/igniteui-angular/src/lib/grids/grid/grid.pipes.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { IgxGridBaseDirective } from '../grid-base.directive';
1313
import { GridType } from '../common/grid.interface';
1414
import { IFilteringStrategy } from '../../data-operations/filtering-strategy';
1515
import { IGridSortingStrategy } from '../../data-operations/sorting-strategy';
16+
import { GridPagingMode } from '../common/enums';
1617

1718
/**
1819
* @hidden
@@ -99,19 +100,19 @@ export class IgxGridPagingPipe implements PipeTransform {
99100
constructor(private gridAPI: GridBaseAPIService<IgxGridBaseDirective & GridType>) { }
100101

101102
public transform(collection: IGroupByResult, page = 0, perPage = 15, id: string, pipeTrigger: number): IGroupByResult {
102-
103-
if (!this.gridAPI.grid.paging) {
103+
if (!this.gridAPI.grid.paging || this.gridAPI.grid.pagingMode !== GridPagingMode.local) {
104104
return collection;
105105
}
106106
const state = {
107107
index: page,
108108
recordsPerPage: perPage
109109
};
110-
DataUtil.correctPagingState(state, collection.data.length);
110+
const total = this.gridAPI.grid._totalRecords >= 0 ? this.gridAPI.grid._totalRecords : collection.data.length;
111+
DataUtil.correctPagingState(state, total);
111112

112113
const result = {
113-
data: DataUtil.page(cloneArray(collection.data), state),
114-
metadata: DataUtil.page(cloneArray(collection.metadata), state)
114+
data: DataUtil.page(cloneArray(collection.data), state, total),
115+
metadata: DataUtil.page(cloneArray(collection.metadata), state, total)
115116
};
116117
if (this.gridAPI.grid.page !== state.index) {
117118
this.gridAPI.grid.page = state.index;

projects/igniteui-angular/src/lib/grids/hierarchical-grid/hierarchical-grid.pipes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { cloneArray } from '../../core/utils';
33
import { GridBaseAPIService } from '../api.service';
44
import { IgxHierarchicalGridComponent } from './hierarchical-grid.component';
55
import { DataUtil } from '../../data-operations/data-util';
6+
import { GridPagingMode } from '../common/enums';
67

78
/**
89
* @hidden
@@ -63,7 +64,7 @@ export class IgxGridHierarchicalPagingPipe implements PipeTransform {
6364

6465
public transform(collection: any[], page = 0, perPage = 15, id: string, pipeTrigger: number): any[] {
6566

66-
if (!this.gridAPI.grid.paging) {
67+
if (!this.gridAPI.grid.paging || this.gridAPI.grid.pagingMode !== GridPagingMode.local) {
6768
return collection;
6869
}
6970

@@ -72,8 +73,10 @@ export class IgxGridHierarchicalPagingPipe implements PipeTransform {
7273
recordsPerPage: perPage
7374
};
7475

75-
const result: any[] = DataUtil.page(cloneArray(collection), state);
76+
const total = this.gridAPI.grid._totalRecords >= 0 ? this.gridAPI.grid._totalRecords : collection.length;
77+
const result: any[] = DataUtil.page(cloneArray(collection), state, total);
7678
this.gridAPI.grid.pagingState = state;
7779
return result;
80+
7881
}
7982
}

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
</div>
175175

176176
<ng-template #defaultPaginator>
177-
<igx-paginator [displayDensity]="displayDensity" [(page)]="page" [totalRecords]="processedExpandedFlatData.length"
177+
<igx-paginator [displayDensity]="displayDensity" [(page)]="page" [totalRecords]="totalRecords"
178178
[(perPage)]="perPage">
179179
</igx-paginator>
180180
</ng-template>

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.pipes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IgxGridBaseDirective } from '../grid/public_api';
99
import { ISortingExpression } from '../../data-operations/sorting-expression.interface';
1010
import { GridType } from '../common/grid.interface';
1111
import { IGridSortingStrategy } from '../../data-operations/sorting-strategy';
12+
import { GridPagingMode } from '../common/enums';
1213

1314
/**
1415
* @hidden
@@ -244,19 +245,19 @@ export class IgxTreeGridPagingPipe implements PipeTransform {
244245

245246
public transform(collection: ITreeGridRecord[], page = 0, perPage = 15, id: string, pipeTrigger: number): ITreeGridRecord[] {
246247
const grid = this.gridAPI.grid;
247-
if (!grid.paging) {
248+
if (!grid.paging || grid.pagingMode !== GridPagingMode.local) {
248249
return collection;
249250
}
250251

251-
const len = collection.length;
252+
const len = grid._totalRecords >= 0 ? grid._totalRecords : collection.length;
252253
const totalPages = Math.ceil(len / perPage);
253254

254255
const state = {
255256
index: (totalPages > 0 && page >= totalPages) ? totalPages - 1 : page,
256257
recordsPerPage: perPage
257258
};
258259

259-
const result: ITreeGridRecord[] = DataUtil.page(cloneArray(collection), state);
260+
const result: ITreeGridRecord[] = DataUtil.page(cloneArray(collection), state, len);
260261
grid.pagingState = state;
261262
(grid as any)._page = state.index;
262263

0 commit comments

Comments
 (0)