Grid Drag & Drop
Grid supports drag-and-drop actions. This feature might be used, for example, to reorder rows and to drag rows between grids.
Drop Mode
The drop mode of a grid determines where a drop can happen. Vaadin offers four different drop modes, which are described in the table here:
| Drop Mode | Description |
|---|---|
Drops can occur on the grid as a whole, not on top of rows or between individual rows. Use this mode when the order isn’t important. | |
Drops can happen between rows. Use this mode when the order is important. | |
Drops can take place on top of rows. This is useful when creating relationships between items or moving an item into another item, such as placing a file inside a folder. | |
On Top or Between | Drops can occur on top of rows or between them. |
Row Reordering
You can drag rows to reorder them. This can be a useful and impressive feature for users. Try dragging with your mouse one of the rows of data in the example here to another place in the list.
Source code
GridRowReordering.java
grid-row-reordering.tsx
grid-row-reordering.ts
Drag Rows between Grids
Rows can be dragged from one grid to another. You might use this feature to move, copy or link items from different datasets.
In the example here, there are two grids of data. Maybe they represent people to speak at two different presentations at the same conference. One grid lists the first panel of speakers and the other the second panel. Try dragging people from one to the other, as if you were reassigning them to speak at a different panel.
Source code
GridDragRowsBetweenGrids.java
grid-drag-rows-between-grids.tsx
grid-drag-rows-between-grids.ts
Drag Multiple Rows
In multi-select mode, dragging a selected row drags the other selected rows with it. The drag image shows the number of dragged rows.
The drag includes only the selected rows that are visible. Selected rows that are scrolled out of view are missing from the dragged items of the drag start event, and the number in the drag image doesn’t count them.
To drag the whole selection, check in the drag start listener whether a selected row is dragged. If it is, use the selected items of the grid instead of the dragged items of the event. Set the number in the drag image to the number of selected rows.
In Flow, set the number with setSelectionDragDetails() before the drag starts, for example in a selection listener. The drag start listener runs on the server only after the browser has created the drag image. In Lit and React, call setDraggedItemsCount() of the event in the drag start listener.
Source code
GridDragMultipleRows.java
package com.vaadin.demo.component.grid;
import java.util.List;
import java.util.Set;
import com.vaadin.demo.domain.DataService;
import com.vaadin.demo.domain.Person;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
public class GridDragMultipleRows extends Div {
private List<Person> draggedItems = List.of();
public GridDragMultipleRows() {
Grid<Person> grid = new Grid<>(Person.class, false);
grid.setSelectionMode(Grid.SelectionMode.MULTI);
grid.setRowsDraggable(true);
// Show the number of all selected rows in the drag image
grid.addSelectionListener(e -> grid.setSelectionDragDetails(
e.getAllSelectedItems().size(),
// No custom drag data
null));
grid.addDragStartListener(e -> {
// The event has only the dragged rows that are visible
List<Person> visibleItems = e.getDraggedItems();
Set<Person> selectedItems = grid.getSelectedItems();
// Dragging a selected row drags the whole selection
draggedItems = selectedItems.containsAll(visibleItems)
? List.copyOf(selectedItems)
: visibleItems;
});
}
}
grid-drag-multiple-rows.tsx
import React, { useEffect } from 'react';
import { useSignal } from '@vaadin/hilla-react-signals';
import { Grid, type GridDragStartEvent } from '@vaadin/react-components/Grid.js';
import { GridColumn } from '@vaadin/react-components/GridColumn.js';
import { GridSelectionColumn } from '@vaadin/react-components/GridSelectionColumn.js';
import { getPeople } from 'Frontend/demo/domain/DataService';
import type Person from 'Frontend/generated/com/vaadin/demo/domain/Person';
function Example() {
const items = useSignal<Person[]>([]);
useEffect(() => {
getPeople().then(({ people }) => {
items.value = people;
});
}, []);
const selectedItems = useSignal<Person[]>([]);
const draggedItems = useSignal<Person[]>([]);
const startDraggingItems = (event: GridDragStartEvent<Person>) => {
// The event has only the dragged rows that are visible
const { draggedItems: visibleItems, setDraggedItemsCount } = event.detail;
// Dragging a selected row drags the whole selection
const selectionDragged = selectedItems.value.includes(visibleItems[0]);
draggedItems.value = selectionDragged ? selectedItems.value : visibleItems;
if (draggedItems.value.length > 1) {
// Show the number of all dragged rows in the drag image
setDraggedItemsCount(draggedItems.value.length);
}
};
return (
<Grid
items={items.value}
rowsDraggable
selectedItems={selectedItems.value}
onSelectedItemsChanged={(event) => {
selectedItems.value = event.detail.value;
}}
onGridDragstart={startDraggingItems}
>
<GridSelectionColumn />
<GridColumn path="firstName" />
<GridColumn path="lastName" />
</Grid>
);
}
grid-drag-multiple-rows.ts
import '@vaadin/grid';
import '@vaadin/grid/vaadin-grid-selection-column.js';
import { html, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import type { GridDragStartEvent, GridSelectedItemsChangedEvent } from '@vaadin/grid';
import { getPeople } from 'Frontend/demo/domain/DataService';
import { applyTheme } from 'Frontend/demo/theme';
import type Person from 'Frontend/generated/com/vaadin/demo/domain/Person';
@customElement('grid-drag-multiple-rows')
export class Example extends LitElement {
protected override createRenderRoot() {
const root = super.createRenderRoot();
applyTheme(root);
return root;
}
@state()
private items: Person[] = [];
protected override async firstUpdated() {
const { people } = await getPeople();
this.items = people;
}
@state()
private selectedItems: Person[] = [];
@state()
private draggedItems: Person[] = [];
private startDraggingItems = (event: GridDragStartEvent<Person>) => {
// The event has only the dragged rows that are visible
const { draggedItems: visibleItems, setDraggedItemsCount } = event.detail;
// Dragging a selected row drags the whole selection
const selectionDragged = this.selectedItems.includes(visibleItems[0]);
this.draggedItems = selectionDragged ? this.selectedItems : visibleItems;
if (this.draggedItems.length > 1) {
// Show the number of all dragged rows in the drag image
setDraggedItemsCount(this.draggedItems.length);
}
};
protected override render() {
return html`
<vaadin-grid
.items="${this.items}"
rows-draggable
.selectedItems="${this.selectedItems}"
@selected-items-changed="${(event: GridSelectedItemsChangedEvent<Person>) => {
this.selectedItems = event.detail.value;
}}"
@grid-dragstart="${this.startDraggingItems}"
>
<vaadin-grid-selection-column></vaadin-grid-selection-column>
<vaadin-grid-column path="firstName"></vaadin-grid-column>
<vaadin-grid-column path="lastName"></vaadin-grid-column>
</vaadin-grid>
`;
}
}
When the drop moves the dragged rows out of the grid, remove them from the selection as well. By default, Grid keeps removed items selected, so the next dragged selection would include them again. In Flow, call grid.setSelectionPreservationMode(SelectionPreservationMode.PRESERVE_EXISTING) to make the grid deselect removed items itself. This mode works only with in-memory data.
The selected items can include rows that a drag filter excludes from dragging. When the grid has a drag filter, leave those items out.
The drag data that the browser transfers with the drag is also created only from the visible rows. To replace it for a dragged selection, pass a map of data types and values as the second argument of setSelectionDragDetails() in Flow. In Lit and React, call setDragData() of the event.
Drag & Drop Filters
Drag-and-drop filters determine which rows are draggable and which rows are valid drop targets. These filters function on a per-row basis.