Skip to content

Commit 7a8c4cc

Browse files
committed
Merge pull request #3857 from lpand/master
#3795 raise sortChanged with proper arguments when grid state is restored
2 parents 1ab77df + cc3139c commit 7a8c4cc

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
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 & 8 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++;
@@ -364,15 +365,13 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
364365
colFilterChangeCount++;
365366
});
366367

367-
grid.api.core.on.sortChanged( $scope, function() {
368-
colSortChangeCount++;
369-
});
368+
grid.api.core.on.sortChanged( $scope, onSortChangedHook );
370369

371370
uiGridSaveStateService.restoreColumns( grid, [
372371
{ name: 'col2', visible: false, width: 90, sort: [ {blah: 'blah'} ], filters: [ {} ] },
373372
{ 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: [ {} ] }
373+
{ name: 'col4', visible: false, width: 120, sort: { direction: 'asc', priority: 1 }, filters: [ {} ] },
374+
{ name: 'col3', visible: true, width: 220, sort: { direction: 'asc', priority: 0 }, filters: [ {} ] }
376375
]);
377376

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

398397
expect( grid.getOnlyDataColumns()[0].sort ).toEqual([ { blah: 'blah' } ]);
399398
expect( grid.getOnlyDataColumns()[1].sort ).toEqual([]);
400-
expect( grid.getOnlyDataColumns()[2].sort ).toEqual([]);
401-
expect( grid.getOnlyDataColumns()[3].sort ).toEqual([]);
399+
expect( grid.getOnlyDataColumns()[2].sort ).toEqual({ direction: 'asc', priority: 1 });
400+
expect( grid.getOnlyDataColumns()[3].sort ).toEqual({ direction: 'asc', priority: 0 });
402401

403402
expect( grid.getOnlyDataColumns()[0].filters ).toEqual([ {} ]);
404403
expect( grid.getOnlyDataColumns()[1].filters ).toEqual([ { blah: 'blah' } ]);
@@ -407,7 +406,13 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
407406

408407
expect( colVisChangeCount ).toEqual( 4, '4 columns changed visibility');
409408
expect( colFilterChangeCount ).toEqual( 1, '1 columns changed filter');
410-
expect( colSortChangeCount ).toEqual( 4, '4 columns changed sort');
409+
410+
expect( onSortChangedHook.calls.length ).toEqual( 1 );
411+
412+
expect( onSortChangedHook ).toHaveBeenCalledWith(
413+
grid,
414+
[ grid.getOnlyDataColumns()[3], grid.getOnlyDataColumns()[2] ]
415+
);
411416
});
412417

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

0 commit comments

Comments
 (0)