GRID getDraggedItems doesn't get all items selected

Hi all,
I’m having a little problem …

when i drag multiple items (more than the ones currently visible on screen for example 20 items selected but only 10 are visible due to its size) in the event only the visible are sent. Is it a bug or am i doing something wrong ?

grid1.addDragStartListener(event -> {

            grid1.setDropMode(GridDropMode.ON_GRID);
            grid2.setDropMode(GridDropMode.ON_GRID);
            draggedItems = event.getDraggedItems();

        }
);

thx in advance for any help.

It’s not a bug, but a limitation: when you drag a selected row, the drag start event only has the selected rows that are visible in the grid. Rows that are scrolled out of view are left out by default.

The server knows the whole selection, so you can take the items from there:

// Show the number of all selected rows in the drag image
grid1.addSelectionListener(e -> grid1.setSelectionDragDetails(
        e.getAllSelectedItems().size(), null));

grid1.addDragStartListener(event -> {
    var visibleItems = event.getDraggedItems();
    var selectedItems = grid1.getSelectedItems();
    // A selected row is dragged: use the whole selection
    draggedItems = selectedItems.containsAll(visibleItems)
            ? List.copyOf(selectedItems)
            : visibleItems;
    grid1.setDropMode(GridDropMode.ON_GRID);
    grid2.setDropMode(GridDropMode.ON_GRID);
});

If the drop moves the rows to the other grid, also deselect them, for example with grid1.asMultiSelect().deselect(draggedItems). Removed items stay selected otherwise.

This wasn’t documented, so I opened a PR to add it to the Grid docs: docs: add section on dragging multiple Grid rows by tomivirkki · Pull Request #6715 · vaadin/docs · GitHub

Hi,

That was the solution that i used but the drag indicator shows the number of dragged items and that may confuse the user … maybe it’s better to remove the number of items dragged

Thx for the clarification.

The selection listener in my snippet should fix it: setSelectionDragDetails(count, null) makes the drag image show the count you give it, so with 20 selected rows it shows 20. Did you add that part too?

I tested it locally and it worked fine:

Oops.

So sorry I didn’t read that part of the code, only looked at the drag listener.

It works fine.

Thx again for helping me.