Skip to content

Commit b038e22

Browse files
committed
raise sortChanged with proper arguments when grid state is restored
1 parent c32abca commit b038e22

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/features/saveState/js/saveState.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@
527527
* @param {object} columnsState the list of columns we had before, with their state
528528
*/
529529
restoreColumns: function( grid, columnsState ){
530+
var isSortChanged = false;
531+
530532
columnsState.forEach( function( columnState, index ) {
531533
var currentCol = grid.getColumn( columnState.name );
532534

533-
534-
535535
if ( currentCol && !grid.isRowHeaderColumn(currentCol) ){
536536
if ( grid.options.saveVisible &&
537537
( currentCol.visible !== columnState.visible ||
@@ -549,7 +549,7 @@
549549
!angular.equals(currentCol.sort, columnState.sort) &&
550550
!( currentCol.sort === undefined && angular.isEmpty(columnState.sort) ) ){
551551
currentCol.sort = angular.copy( columnState.sort );
552-
grid.api.core.raise.sortChanged();
552+
isSortChanged = true;
553553
}
554554

555555
if ( grid.options.saveFilter &&
@@ -576,6 +576,10 @@
576576
}
577577
}
578578
});
579+
580+
if ( isSortChanged ) {
581+
grid.api.core.raise.sortChanged( grid, grid.getColumnSorting() );
582+
}
579583
},
580584

581585

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
355355
var colVisChangeCount = 0;
356356
var colFilterChangeCount = 0;
357357
var colSortChangeCount = 0;
358+
var onSortChangedHook = jasmine.createSpy('onSortChangedHook');
358359

359360
grid.api.core.on.columnVisibilityChanged( $scope, function( column ) {
360361
colVisChangeCount++;
@@ -368,11 +369,13 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
368369
colSortChangeCount++;
369370
});
370371

372+
grid.api.core.on.sortChanged( $scope, onSortChangedHook );
373+
371374
uiGridSaveStateService.restoreColumns( grid, [
372375
{ name: 'col2', visible: false, width: 90, sort: [ {blah: 'blah'} ], filters: [ {} ] },
373376
{ name: 'col1', visible: true, width: '*', sort: [], filters: [ {'blah': 'blah'} ] },
374-
{ name: 'col4', visible: false, width: 120, sort: [], filters: [ {} ] },
375-
{ name: 'col3', visible: true, width: 220, sort: [], filters: [ {} ] }
377+
{ name: 'col4', visible: false, width: 120, sort: { direction: 'asc', priority: 1 }, filters: [ {} ] },
378+
{ name: 'col3', visible: true, width: 220, sort: { direction: 'asc', priority: 0 }, filters: [ {} ] }
376379
]);
377380

378381
expect( grid.getOnlyDataColumns()[0].name ).toEqual('col2', 'column 0 name should be col2');
@@ -397,8 +400,8 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
397400

398401
expect( grid.getOnlyDataColumns()[0].sort ).toEqual([ { blah: 'blah' } ]);
399402
expect( grid.getOnlyDataColumns()[1].sort ).toEqual([]);
400-
expect( grid.getOnlyDataColumns()[2].sort ).toEqual([]);
401-
expect( grid.getOnlyDataColumns()[3].sort ).toEqual([]);
403+
expect( grid.getOnlyDataColumns()[2].sort ).toEqual({ direction: 'asc', priority: 1 });
404+
expect( grid.getOnlyDataColumns()[3].sort ).toEqual({ direction: 'asc', priority: 0 });
402405

403406
expect( grid.getOnlyDataColumns()[0].filters ).toEqual([ {} ]);
404407
expect( grid.getOnlyDataColumns()[1].filters ).toEqual([ { blah: 'blah' } ]);
@@ -407,7 +410,12 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
407410

408411
expect( colVisChangeCount ).toEqual( 4, '4 columns changed visibility');
409412
expect( colFilterChangeCount ).toEqual( 1, '1 columns changed filter');
410-
expect( colSortChangeCount ).toEqual( 4, '4 columns changed sort');
413+
expect( colSortChangeCount ).toEqual( 1, '1 columns changed sort');
414+
415+
expect( onSortChangedHook ).toHaveBeenCalledWith(
416+
grid,
417+
[ grid.getOnlyDataColumns()[3], grid.getOnlyDataColumns()[2] ]
418+
);
411419
});
412420

413421
it('restore columns, all options turned off', function() {

0 commit comments

Comments
 (0)