Skip to content

Commit e4b2293

Browse files
committed
Merge pull request #2490 from edmondpr/fix-column-move-horizontal-scroll
Fix #2489: Column moving not working well with horizontal scrolling
2 parents e9600ed + 16c0d65 commit e4b2293

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@
291291
//Hide column menu
292292
uiGridCtrl.fireEvent('hide-menu');
293293

294+
//Calculate total column width
295+
var columns = $scope.grid.columns;
296+
var totalColumnWidth = 0;
297+
for (var i = 0; i < columns.length; i++) {
298+
if (angular.isUndefined(columns[i].colDef.visible) || columns[i].colDef.visible === true) {
299+
totalColumnWidth += columns[i].drawnWidth || columns[i].width || columns[i].colDef.width;
300+
}
301+
}
302+
294303
//Calculate new position of left of column
295304
var currentElmLeft = movingElm[0].getBoundingClientRect().left - 1;
296305
var currentElmRight = movingElm[0].getBoundingClientRect().right;
@@ -307,13 +316,31 @@
307316
if ((currentElmLeft >= gridLeft || changeValue > 0) && (currentElmRight <= rightMoveLimit || changeValue < 0)) {
308317
movingElm.css({visibility: 'visible', 'left': newElementLeft + 'px'});
309318
}
310-
else {
319+
else if (totalColumnWidth > Math.ceil(uiGridCtrl.grid.gridWidth)) {
311320
changeValue *= 8;
312321
var scrollEvent = new ScrollEvent($scope.col.grid, null, null, 'uiGridHeaderCell.moveElement');
313322
scrollEvent.x = {pixels: changeValue};
314323
scrollEvent.fireScrollingEvent();
315324
}
316-
totalMouseMovement += changeValue;
325+
326+
//Calculate total width of columns on the left of the moving column and the mouse movement
327+
var totalColumnsLeftWidth = 0;
328+
for (var il = 0; il < columns.length; il++) {
329+
if (angular.isUndefined(columns[il].colDef.visible) || columns[il].colDef.visible === true) {
330+
if (columns[il].colDef.name !== $scope.col.colDef.name) {
331+
totalColumnsLeftWidth += columns[il].drawnWidth || columns[il].width || columns[il].colDef.width;
332+
}
333+
else {
334+
break;
335+
}
336+
}
337+
}
338+
if ($scope.newScrollLeft === undefined) {
339+
totalMouseMovement += changeValue;
340+
}
341+
else {
342+
totalMouseMovement = $scope.newScrollLeft + newElementLeft - totalColumnsLeftWidth;
343+
}
317344

318345
//Increase width of moving column, in case the rightmost column was moved and its width was
319346
//decreased because of overflow
@@ -324,6 +351,9 @@
324351
};
325352

326353
var mouseMoveHandler = function (evt) {
354+
//Disable text selection in Chrome during column move
355+
document.onselectstart = function() { return false; };
356+
327357
var changeValue = evt.pageX - previousMouseX;
328358
if (!elmCloned && Math.abs(changeValue) > 50) {
329359
cloneElement();
@@ -345,6 +375,8 @@
345375
$document.on('mousemove', mouseMoveHandler);
346376

347377
var mouseUpHandler = function (evt) {
378+
//Re-enable text selection after column move
379+
document.onselectstart = null;
348380

349381
//Remove the cloned element on mouse up.
350382
if (movingElm) {

src/js/core/directives/ui-grid-render-container.js

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
containerCtrl.prevScrollArgs = args;
100100
var newScrollLeft = args.getNewScrollLeft(colContainer,containerCtrl.viewport);
101101

102+
// Make the current horizontal scroll position available in the $scope
103+
$scope.newScrollLeft = newScrollLeft;
104+
102105
if (containerCtrl.headerViewport) {
103106
containerCtrl.headerViewport.scrollLeft = gridUtil.denormalizeScrollLeft(containerCtrl.headerViewport, newScrollLeft);
104107
}

src/js/core/factories/GridRenderContainer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,16 @@ angular.module('ui.grid')
410410

411411
var newRange = [];
412412
if (columnCache.length > self.grid.options.columnVirtualizationThreshold && self.getCanvasWidth() > self.getViewportWidth()) {
413+
/* Commented the following lines because otherwise the moved column wasn't visible immediately on the new position
414+
* in the case of many columns with horizontal scroll, one had to scroll left or right and then return in order to see it
413415
// Have we hit the threshold going down?
414416
if (self.prevScrollLeft < scrollLeft && colIndex < self.prevColumnScrollIndex + self.grid.options.horizontalScrollThreshold && colIndex < maxColumnIndex) {
415417
return;
416418
}
417419
//Have we hit the threshold going up?
418420
if (self.prevScrollLeft > scrollLeft && colIndex > self.prevColumnScrollIndex - self.grid.options.horizontalScrollThreshold && colIndex < maxColumnIndex) {
419421
return;
420-
}
422+
}*/
421423

422424
var rangeStart = Math.max(0, colIndex - self.grid.options.excessColumns);
423425
var rangeEnd = Math.min(columnCache.length, colIndex + minCols + self.grid.options.excessColumns);

0 commit comments

Comments
 (0)