Skip to content

Commit c6d3b2a

Browse files
author
David Jellesma
committed
fix(saveState): Fixes #4146 - Allow saving of pagination state
1 parent 47c77de commit c6d3b2a

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/features/saveState/js/saveState.js

+44
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@
287287
savedState.selection = service.saveSelection( grid );
288288
savedState.grouping = service.saveGrouping( grid );
289289
savedState.treeView = service.saveTreeView( grid );
290+
savedState.pagination = service.savePagination( grid );
290291

291292
return savedState;
292293
},
@@ -323,6 +324,10 @@
323324
service.restoreTreeView( grid, state.treeView );
324325
}
325326

327+
if ( state.pagination ){
328+
service.restorePagination( grid, state.pagination );
329+
}
330+
326331
grid.refresh();
327332
},
328333

@@ -474,6 +479,26 @@
474479
},
475480

476481

482+
/**
483+
* @ngdoc function
484+
* @name savePagination
485+
* @methodOf ui.grid.saveState.service:uiGridSaveStateService
486+
* @description Saves the pagination state, if the pagination feature is enabled
487+
* @param {Grid} grid the grid whose state we'd like to save
488+
* @returns {object} the pagination state ready to be saved
489+
*/
490+
savePagination: function( grid ) {
491+
if ( !grid.api.pagination || !grid.options.paginationPageSize ){
492+
return {};
493+
}
494+
495+
return {
496+
paginationCurrentPage: grid.options.paginationCurrentPage,
497+
paginationPageSize: grid.options.paginationPageSize
498+
};
499+
},
500+
501+
477502
/**
478503
* @ngdoc function
479504
* @name saveTreeView
@@ -698,6 +723,25 @@
698723
grid.api.treeView.setTreeView( treeViewState );
699724
},
700725

726+
/**
727+
* @ngdoc function
728+
* @name restorePagination
729+
* @methodOf ui.grid.saveState.service:uiGridSaveStateService
730+
* @description Restores the pagination information, if pagination is enabled.
731+
* @param {Grid} grid the grid whose state we'd like to restore
732+
* @param {object} pagination the pagination object to be restored
733+
* @param {number} pagination.paginationCurrentPage the page number to restore
734+
* @param {number} pagination.paginationPageSize the number of items displayed per page
735+
*/
736+
restorePagination: function( grid, pagination ){
737+
if ( !grid.api.pagination || !grid.options.paginationPageSize ){
738+
return;
739+
}
740+
741+
grid.options.paginationCurrentPage = pagination.paginationCurrentPage;
742+
grid.options.paginationPageSize = pagination.paginationPageSize;
743+
},
744+
701745
/**
702746
* @ngdoc function
703747
* @name findRowByIdentity

src/features/saveState/test/saveState.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
189189
});
190190
});
191191

192+
describe('savePagination', function() {
193+
beforeEach(function() {
194+
grid.options.paginationPageSize = 25;
195+
grid.options.paginationCurrentPage = 2;
196+
grid.api.pagination = true;
197+
});
198+
199+
it('saves paginationCurrentPage', function() {
200+
expect(uiGridSaveStateService.savePagination( grid ) ).toEqual({
201+
paginationCurrentPage: 2,
202+
paginationPageSize: 25
203+
});
204+
});
205+
206+
});
207+
192208

193209
describe('saveScrollFocus', function() {
194210
it('does nothing when no cellNav module initialized', function() {
@@ -482,6 +498,46 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
482498
});
483499
});
484500

501+
describe('restorePagination', function() {
502+
var pagination = {
503+
paginationCurrentPage: 2,
504+
paginationPageSize: 25
505+
};
506+
507+
describe('when pagination is on', function() {
508+
beforeEach(function() {
509+
grid.options.paginationPageSize = 1;
510+
grid.api.pagination = true;
511+
uiGridSaveStateService.restorePagination( grid, pagination );
512+
});
513+
514+
it('sets the paginationPageSize', function() {
515+
expect(grid.options.paginationPageSize).toEqual(25);
516+
});
517+
518+
it('sets the paginationCurrentPage', function() {
519+
expect(grid.options.paginationCurrentPage).toEqual(2);
520+
});
521+
});
522+
523+
describe('when pagination is off', function() {
524+
beforeEach(function() {
525+
grid.api.pagination = false;
526+
uiGridSaveStateService.restorePagination( grid, pagination );
527+
});
528+
529+
it('does not modify paginationPageSize', function() {
530+
expect(grid.options.paginationPageSize).toBeUndefined();
531+
});
532+
533+
it('does not modify paginationCurrentPage', function() {
534+
expect(grid.options.paginationCurrentPage).toBeUndefined();
535+
});
536+
537+
538+
});
539+
});
540+
485541

486542
describe('restoreScrollFocus', function() {
487543
it('does nothing when no cellNav module initialized', function() {

0 commit comments

Comments
 (0)