From c81f56fcda5a75b237b1119c4912816ef05cd827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Wed, 19 Oct 2022 10:33:26 +0200 Subject: [PATCH] fix: swap columns iteratively on reorder (#2220) * fix: swap columns iteratively on reorder Cherry-pick of https://github.com/vaadin/web-components/pull/4534 * replace usages of findIndex, includes * try reduce column size * Update src/vaadin-grid-column-reordering-mixin.html Co-authored-by: Tomi Virkki Co-authored-by: Tomi Virkki --- src/vaadin-grid-column-reordering-mixin.html | 40 +++++++++++++++--- test/column-reordering.html | 44 ++++++++++++++++++-- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/vaadin-grid-column-reordering-mixin.html b/src/vaadin-grid-column-reordering-mixin.html index 9c71d2e17..2298de4f1 100644 --- a/src/vaadin-grid-column-reordering-mixin.html +++ b/src/vaadin-grid-column-reordering-mixin.html @@ -167,7 +167,23 @@ const targetColumn = this._getTargetColumn(targetCell, this._draggedColumn); if (this._isSwapAllowed(this._draggedColumn, targetColumn) && this._isSwappableByPosition(targetColumn, e.detail.x)) { - this._swapColumnOrders(this._draggedColumn, targetColumn); + // Get the header level of the target column (and the dragged column) + const columnTreeLevel = this.__getColumnTreeLevel(targetColumn); + // Get the columns on that level in visual order + const levelColumnsInOrder = this._getColumnsInOrder(columnTreeLevel); + + // Index of the column being dragged + const startIndex = levelColumnsInOrder.indexOf(this._draggedColumn); + // Index of the column being dragged over + const endIndex = levelColumnsInOrder.indexOf(targetColumn); + + // Direction of iteration + const direction = startIndex < endIndex ? 1 : -1; + + // Iteratively swap all the columns from the dragged column to the target column + for (let i = startIndex; i !== endIndex; i += direction) { + this._swapColumnOrders(this._draggedColumn, levelColumnsInOrder[i + direction]); + } } this._updateGhostPosition(e.detail.x, this._touchDevice ? e.detail.y - 50 : e.detail.y); @@ -194,13 +210,17 @@ } /** + * Returns the columns (or column groups) on the specified header level in visual order. + * By default, the bottom level is used. + * * @return {!Array} * @protected */ - _getColumnsInOrder() { - return this._columnTree.slice(0).pop() - .filter(c => !c.hidden) - .sort((b, a) => (b._order - a._order)); + _getColumnsInOrder(headerLevel) { + if (!headerLevel && headerLevel !== 0) { + headerLevel = this._columnTree.length - 1; + } + return this._columnTree[headerLevel].filter((c) => !c.hidden).sort((b, a) => b._order - a._order); } /** @@ -403,6 +423,16 @@ } } + /** @private */ + __getColumnTreeLevel(column) { + for (let level = 0; level < this._columnTree.length; level++) { + if (this._columnTree[level].indexOf(column) >= 0) { + return level; + } + } + return -1; + } + /** * Fired when the columns in the grid are reordered. * diff --git a/test/column-reordering.html b/test/column-reordering.html index 4fd7ffa36..acd8ddf58 100644 --- a/test/column-reordering.html +++ b/test/column-reordering.html @@ -41,15 +41,15 @@