Skip to content

Commit 17296cd

Browse files
committed
fix: Wrong sort priorities 4653 and 4196
4653: sort Priority 1 is presented as 0 in the sort object. It shows true for !column.sort.priority so it should be checked with column.sort.priority === undefined. 4196: add function to decrease higher priorities if a column sort with lower priority is removed. Added test cases to verify behaviour.
1 parent 68681d7 commit 17296cd

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/js/core/factories/Grid.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ angular.module('ui.grid')
19791979
// Get the actual priority since there may be columns which have suppressRemoveSort set
19801980
column.sort.priority = self.getNextColumnSortPriority();
19811981
}
1982-
else if (!column.sort.priority){
1982+
else if (column.sort.priority === undefined){
19831983
column.sort.priority = self.getNextColumnSortPriority();
19841984
}
19851985

@@ -1997,7 +1997,7 @@ angular.module('ui.grid')
19971997
if (column.sortDirectionCycle[i]) {
19981998
column.sort.direction = column.sortDirectionCycle[i];
19991999
} else {
2000-
column.sort = {};
2000+
removeSortOfColumn(column, self);
20012001
}
20022002
}
20032003
else {
@@ -2009,6 +2009,18 @@ angular.module('ui.grid')
20092009
return $q.when(column);
20102010
};
20112011

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

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

+26
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)