Skip to content

Commit 2d433bb

Browse files
committed
fix(moveColumns) Restore column order after altering columnDefs array.
Cache the column order when columns are moved, and restore that order using a dataChangeCallback. Fixes #4254
1 parent f2ed365 commit 2d433bb

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/features/move-columns/js/column-movable.js

+31
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
var self = this;
2828
this.registerPublicApi(grid);
2929
this.defaultGridOptions(grid.options);
30+
grid.moveColumns = {orderCache: []}; // Used to cache the order before columns are rebuilt
3031
grid.registerColumnBuilder(self.movableColumnBuilder);
32+
grid.registerDataChangeCallback(self.verifyColumnOrder, [uiGridConstants.dataChange.COLUMN]);
3133
},
3234
registerPublicApi: function (grid) {
3335
var self = this;
@@ -137,6 +139,34 @@
137139
: colDef.enableColumnMoving;
138140
return $q.all(promises);
139141
},
142+
/**
143+
* @ngdoc method
144+
* @name updateColumnCache
145+
* @methodOf ui.grid.moveColumns
146+
* @description Cache the current order of columns, so we can restore them after new columnDefs are defined
147+
*/
148+
updateColumnCache: function(grid){
149+
grid.moveColumns.orderCache = grid.getOnlyDataColumns();
150+
},
151+
/**
152+
* @ngdoc method
153+
* @name verifyColumnOrder
154+
* @methodOf ui.grid.moveColumns
155+
* @description dataChangeCallback which uses the cached column order to restore the column order
156+
* when it is reset by altering the columnDefs array.
157+
*/
158+
verifyColumnOrder: function(grid){
159+
var headerRowOffset = grid.rowHeaderColumns.length;
160+
var newIndex;
161+
162+
angular.forEach(grid.moveColumns.orderCache, function(cacheCol, cacheIndex){
163+
newIndex = grid.columns.indexOf(cacheCol);
164+
if ( newIndex !== -1 && newIndex - headerRowOffset !== cacheIndex ){
165+
var column = grid.columns.splice(newIndex, 1)[0];
166+
grid.columns.splice(cacheIndex + headerRowOffset, 0, column);
167+
}
168+
});
169+
},
140170
redrawColumnAtPosition: function (grid, originalPosition, newPosition) {
141171

142172
var columns = grid.columns;
@@ -154,6 +184,7 @@
154184
}
155185
}
156186
columns[newPosition] = originalColumn;
187+
service.updateColumnCache(grid);
157188
grid.queueGridRefresh();
158189
$timeout(function () {
159190
grid.api.core.notifyDataChange( uiGridConstants.dataChange.COLUMN );

src/features/move-columns/test/column-movable.spec.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe('ui.grid.moveColumns', function () {
22

3-
var scope, element, timeout, gridUtil, document;
3+
var scope, element, timeout, gridUtil, document, uiGridConstants;
44

55
var data = [
66
{ "name": "Ethel Price", "gender": "female", "age": 25, "company": "Enersol", phone: '111'},
@@ -11,14 +11,14 @@ describe('ui.grid.moveColumns', function () {
1111

1212
beforeEach(module('ui.grid.moveColumns'));
1313

14-
beforeEach(inject(function (_$compile_, $rootScope, $timeout, _gridUtil_, $document) {
14+
beforeEach(inject(function (_$compile_, $rootScope, $timeout, _gridUtil_, $document, _uiGridConstants_) {
1515

1616
var $compile = _$compile_;
1717
scope = $rootScope;
1818
timeout = $timeout;
1919
gridUtil = _gridUtil_;
2020
document = $document;
21-
21+
uiGridConstants = _uiGridConstants_;
2222

2323
scope.gridOptions = {};
2424
scope.gridOptions.data = data;
@@ -74,6 +74,21 @@ describe('ui.grid.moveColumns', function () {
7474
expect(scope.grid.columns[4].name).toBe('gender');
7575
});
7676

77+
it('expect moveColumn() to persist after adding additional column', function () {
78+
scope.gridApi.colMovable.moveColumn(0, 1);
79+
scope.gridOptions.columnDefs.push({ field: 'name', displayName: 'name2', width: 200 });
80+
scope.gridApi.core.notifyDataChange( uiGridConstants.COLUMN );
81+
timeout.flush();
82+
scope.$digest();
83+
84+
expect(scope.grid.columns[0].name).toBe('gender');
85+
expect(scope.grid.columns[1].name).toBe('name');
86+
expect(scope.grid.columns[2].name).toBe('age');
87+
expect(scope.grid.columns[3].name).toBe('company');
88+
expect(scope.grid.columns[4].name).toBe('phone');
89+
expect(scope.grid.columns[5].displayName).toBe('name2');
90+
});
91+
7792
it('expect moveColumn() to not change position of columns if enableColumnMoving is false', function () {
7893
scope.gridApi.colMovable.moveColumn(2, 1);
7994
expect(scope.grid.columns[0].name).toBe('name');

0 commit comments

Comments
 (0)