Skip to content

Commit e0a6a67

Browse files
authored
fix: swap grid columns iteratively on reorder (#4534)
1 parent 7f35903 commit e0a6a67

2 files changed

Lines changed: 61 additions & 10 deletions

File tree

‎packages/grid/src/vaadin-grid-column-reordering-mixin.js‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,23 @@ export const ColumnReorderingMixin = (superClass) =>
161161
this._isSwapAllowed(this._draggedColumn, targetColumn) &&
162162
this._isSwappableByPosition(targetColumn, e.detail.x)
163163
) {
164-
this._swapColumnOrders(this._draggedColumn, targetColumn);
164+
// Get the column header level of the target column (and the dragged column)
165+
const columnTreeLevel = this._columnTree.findIndex((level) => level.includes(targetColumn));
166+
// Get the columns on that level in visual order
167+
const levelColumnsInOrder = this._getColumnsInOrder(columnTreeLevel);
168+
169+
// Index of the column being dragged
170+
const startIndex = levelColumnsInOrder.indexOf(this._draggedColumn);
171+
// Index of the column being dragged over
172+
const endIndex = levelColumnsInOrder.indexOf(targetColumn);
173+
174+
// Direction of iteration
175+
const direction = startIndex < endIndex ? 1 : -1;
176+
177+
// Iteratively swap all the columns from the dragged column to the target column
178+
for (let i = startIndex; i !== endIndex; i += direction) {
179+
this._swapColumnOrders(this._draggedColumn, levelColumnsInOrder[i + direction]);
180+
}
165181
}
166182

167183
this._updateGhostPosition(e.detail.x, this._touchDevice ? e.detail.y - 50 : e.detail.y);
@@ -192,15 +208,14 @@ export const ColumnReorderingMixin = (superClass) =>
192208
}
193209

194210
/**
211+
* Returns the columns (or column groups) on the specified header level in visual order.
212+
* By default, the bottom level is used.
213+
*
195214
* @return {!Array<!GridColumn>}
196215
* @protected
197216
*/
198-
_getColumnsInOrder() {
199-
return this._columnTree
200-
.slice(0)
201-
.pop()
202-
.filter((c) => !c.hidden)
203-
.sort((b, a) => b._order - a._order);
217+
_getColumnsInOrder(headerLevel = this._columnTree.length - 1) {
218+
return this._columnTree[headerLevel].filter((c) => !c.hidden).sort((b, a) => b._order - a._order);
204219
}
205220

206221
/**

‎packages/grid/test/column-reordering.test.js‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,32 @@ describe('reordering simple grid', () => {
233233
});
234234

235235
it('should reorder multiple columns while dragging', () => {
236+
// Start column order [1, 2, 3, 4]
237+
238+
// Drag column 1 on top of column 2. Expected order [2, 1, 3, 4]
236239
dragOver(headerContent[0], headerContent[1]);
240+
// Keep dragging the column all the way over to the last column. Expected order [2, 3, 4, 1]
237241
dragOver(headerContent[0], headerContent[3]);
238242
flushGrid(grid);
239-
expectVisualOrder(grid, [2, 4, 3, 1]);
243+
expectVisualOrder(grid, [2, 3, 4, 1]);
244+
});
245+
246+
it('should shift columns in between when dragged as last', () => {
247+
// Start column order [1, 2, 3, 4]
248+
249+
// Drag column 2 all the way over to the last column. Expected order [1, 3, 4, 2]
250+
dragOver(headerContent[1], headerContent[3]);
251+
flushGrid(grid);
252+
expectVisualOrder(grid, [1, 3, 4, 2]);
253+
});
254+
255+
it('should shift columns in between when dragged as first', () => {
256+
// Start column order [1, 2, 3, 4]
257+
258+
// Drag column 3 over the very first column. Expected order [3, 1, 2, 4]
259+
dragOver(headerContent[2], headerContent[0]);
260+
flushGrid(grid);
261+
expectVisualOrder(grid, [3, 1, 2, 4]);
240262
});
241263

242264
it('should update first-column attribute', () => {
@@ -378,8 +400,8 @@ describe('reordering grid with columns groups', () => {
378400

379401
beforeEach(async () => {
380402
grid = fixtureSync(`
381-
<vaadin-grid style="width: 400px; height: 200px;" size="1" column-reordering-allowed>
382-
${[1, 2].map((colgroup) => {
403+
<vaadin-grid style="width: 800px; height: 200px;" size="1" column-reordering-allowed>
404+
${[1, 2, 3].map((colgroup) => {
383405
return `
384406
<vaadin-grid-column-group>
385407
<template class="header">${colgroup}</template>
@@ -425,6 +447,20 @@ describe('reordering grid with columns groups', () => {
425447
expectVisualOrder(grid, [21, 22, 11, 12]);
426448
});
427449

450+
it('should reorder all the groups', () => {
451+
// Start order
452+
// [1, 2, 3 ] <- groups
453+
// [11, 12, 21, 22, 31, 32] <- columns
454+
455+
// Drag the first group over the last group
456+
dragAndDropOver(getVisualHeaderCellContent(grid, 0, 0), getVisualHeaderCellContent(grid, 0, 5));
457+
458+
// Expected order
459+
// [2, 3, 1 ] <- groups
460+
// [21, 22, 31, 32, 11, 12] <- columns
461+
expectVisualOrder(grid, [21, 22, 31, 32, 11, 12]);
462+
});
463+
428464
it('should allow dropping group over other groups column header', () => {
429465
dragAndDropOver(getVisualHeaderCellContent(grid, 0, 0), getVisualHeaderCellContent(grid, 1, 3));
430466
expectVisualOrder(grid, [21, 22, 11, 12]);

0 commit comments

Comments
 (0)