Skip to content

fix(moveColumns) Restore column order after altering columnDefs array. #4276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/features/move-columns/js/column-movable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
var self = this;
this.registerPublicApi(grid);
this.defaultGridOptions(grid.options);
grid.moveColumns = {orderCache: []}; // Used to cache the order before columns are rebuilt
grid.registerColumnBuilder(self.movableColumnBuilder);
grid.registerDataChangeCallback(self.verifyColumnOrder, [uiGridConstants.dataChange.COLUMN]);
},
registerPublicApi: function (grid) {
var self = this;
Expand Down Expand Up @@ -137,6 +139,34 @@
: colDef.enableColumnMoving;
return $q.all(promises);
},
/**
* @ngdoc method
* @name updateColumnCache
* @methodOf ui.grid.moveColumns
* @description Cache the current order of columns, so we can restore them after new columnDefs are defined
*/
updateColumnCache: function(grid){
grid.moveColumns.orderCache = grid.getOnlyDataColumns();
},
/**
* @ngdoc method
* @name verifyColumnOrder
* @methodOf ui.grid.moveColumns
* @description dataChangeCallback which uses the cached column order to restore the column order
* when it is reset by altering the columnDefs array.
*/
verifyColumnOrder: function(grid){
var headerRowOffset = grid.rowHeaderColumns.length;
var newIndex;

angular.forEach(grid.moveColumns.orderCache, function(cacheCol, cacheIndex){
newIndex = grid.columns.indexOf(cacheCol);
if ( newIndex !== -1 && newIndex - headerRowOffset !== cacheIndex ){
var column = grid.columns.splice(newIndex, 1)[0];
grid.columns.splice(cacheIndex + headerRowOffset, 0, column);
}
});
},
redrawColumnAtPosition: function (grid, originalPosition, newPosition) {

var columns = grid.columns;
Expand All @@ -154,6 +184,7 @@
}
}
columns[newPosition] = originalColumn;
service.updateColumnCache(grid);
grid.queueGridRefresh();
$timeout(function () {
grid.api.core.notifyDataChange( uiGridConstants.dataChange.COLUMN );
Expand Down
21 changes: 18 additions & 3 deletions src/features/move-columns/test/column-movable.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('ui.grid.moveColumns', function () {

var scope, element, timeout, gridUtil, document;
var scope, element, timeout, gridUtil, document, uiGridConstants;

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

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

beforeEach(inject(function (_$compile_, $rootScope, $timeout, _gridUtil_, $document) {
beforeEach(inject(function (_$compile_, $rootScope, $timeout, _gridUtil_, $document, _uiGridConstants_) {

var $compile = _$compile_;
scope = $rootScope;
timeout = $timeout;
gridUtil = _gridUtil_;
document = $document;

uiGridConstants = _uiGridConstants_;

scope.gridOptions = {};
scope.gridOptions.data = data;
Expand Down Expand Up @@ -74,6 +74,21 @@ describe('ui.grid.moveColumns', function () {
expect(scope.grid.columns[4].name).toBe('gender');
});

it('expect moveColumn() to persist after adding additional column', function () {
scope.gridApi.colMovable.moveColumn(0, 1);
scope.gridOptions.columnDefs.push({ field: 'name', displayName: 'name2', width: 200 });
scope.gridApi.core.notifyDataChange( uiGridConstants.COLUMN );
timeout.flush();
scope.$digest();

expect(scope.grid.columns[0].name).toBe('gender');
expect(scope.grid.columns[1].name).toBe('name');
expect(scope.grid.columns[2].name).toBe('age');
expect(scope.grid.columns[3].name).toBe('company');
expect(scope.grid.columns[4].name).toBe('phone');
expect(scope.grid.columns[5].displayName).toBe('name2');
});

it('expect moveColumn() to not change position of columns if enableColumnMoving is false', function () {
scope.gridApi.colMovable.moveColumn(2, 1);
expect(scope.grid.columns[0].name).toBe('name');
Expand Down