Skip to content

Commit f4d1dcf

Browse files
authored
Merge pull request #5343 from jgrasl/bugfix/wrong-sort-priorities-after-existing-sort-change
Fix: bug wrong sort priorities fix #4653 and fix #4196
2 parents 84420ab + 17296cd commit f4d1dcf

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/js/core/factories/Grid.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ angular.module('ui.grid')
19801980
// Get the actual priority since there may be columns which have suppressRemoveSort set
19811981
column.sort.priority = self.getNextColumnSortPriority();
19821982
}
1983-
else if (!column.sort.priority){
1983+
else if (column.sort.priority === undefined){
19841984
column.sort.priority = self.getNextColumnSortPriority();
19851985
}
19861986

@@ -1998,7 +1998,7 @@ angular.module('ui.grid')
19981998
if (column.sortDirectionCycle[i]) {
19991999
column.sort.direction = column.sortDirectionCycle[i];
20002000
} else {
2001-
column.sort = {};
2001+
removeSortOfColumn(column, self);
20022002
}
20032003
}
20042004
else {
@@ -2010,6 +2010,18 @@ angular.module('ui.grid')
20102010
return $q.when(column);
20112011
};
20122012

2013+
var removeSortOfColumn = function removeSortOfColumn(column, grid) {
2014+
//Decrease priority for every col where priority is higher than the removed sort's priority.
2015+
grid.columns.forEach(function (col) {
2016+
if (col.sort && col.sort.priority !== undefined && col.sort.priority > column.sort.priority) {
2017+
col.sort.priority -= 1;
2018+
}
2019+
});
2020+
2021+
//Remove sort
2022+
column.sort = {};
2023+
};
2024+
20132025
/**
20142026
* communicate to outside world that we are done with initial rendering
20152027
*/

test/unit/core/factories/Grid.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,32 @@ describe('Grid factory', function () {
810810
expect( column.sort.direction ).toEqual(uiGridConstants.ASC);
811811
expect( column.sort.priority ).toEqual(0);
812812
});
813+
814+
it( 'if two column has sort 1 and 2 on the ui which is 0 and 1 in the sort object and the sort change for the first do not change the priority', function() {
815+
var priorColumn1 = new GridColumn({ name: 'a', sort: { direction: uiGridConstants.ASC, priority: 0 } });
816+
var priorColumn2 = new GridColumn({ name: 'b', sort: { direction: uiGridConstants.ASC, priority: 1 } });
817+
grid.columns.push( priorColumn1 );
818+
grid.columns.push( priorColumn2 );
819+
820+
grid.sortColumn( priorColumn1, true );
821+
822+
expect( priorColumn1.sort ).toEqual({ direction: uiGridConstants.DESC, priority: 0});
823+
});
824+
825+
it( 'if three column has sort 1,2 and 3 on the ui which is 0,1 and 2 in the sort object and the sort removed for the second decrease priority for the third but do not change for the first', function() {
826+
var priorColumn1 = new GridColumn({ name: 'a', sort: { direction: uiGridConstants.ASC, priority: 0 } });
827+
var priorColumn2 = new GridColumn({ name: 'b', sort: { direction: uiGridConstants.DESC, priority: 1 } });
828+
var priorColumn3 = new GridColumn({ name: 'c', sort: { direction: uiGridConstants.ASC, priority: 2 } });
829+
grid.columns.push( priorColumn1 );
830+
grid.columns.push( priorColumn2 );
831+
grid.columns.push( priorColumn3 );
832+
833+
grid.sortColumn( priorColumn2, true );
834+
835+
expect( priorColumn1.sort ).toEqual({ direction: uiGridConstants.ASC, priority: 0 });
836+
expect( priorColumn2.sort ).toEqual({ });
837+
expect( priorColumn3.sort ).toEqual({ direction: uiGridConstants.ASC, priority: 1 });
838+
});
813839
});
814840

815841

0 commit comments

Comments
 (0)