Search code examples
javadrag-and-dropvaadin14

Drag and drop between two grids in Vaadin 14


I want to run the example from the official Vaadin vendor site: https://vaadin.com/components/vaadin-grid/java-examples/drag-and-drop Of course, I have some other objects to drag, but i am happy when the example is working. At the minute, I don't know how it is meant to be used, or I am getting dumb, because I am living in germany.

I wonder why the example is not running out of the box. When i use this code, there are several errors in the code. What is dragItems? And how to calculate the index of the drop position? Any help is appreciated. I just want to use this framework, but the demos are not compiling.

I want to drag an item from a grid on another grid. How to do that?


Solution

  • These fields are missing in the code:

    private Collection<Person> draggedItems;
    private Person draggedItem;
    private Grid<Person> dragSource;
    

    You can check the full code here for Vaadin 14: https://github.com/vaadin/vaadin-grid-flow/blob/4.3/vaadin-grid-flow-demo/src/main/java/com/vaadin/flow/component/grid/demo/GridDemo.java#L2375

    • draggedItems is the list of items that you started to drag (they are set on GridDragStartEvent and cleaned on GridDragEndEvent)
    • You are dropping the items on an item (before or after). The index of the position has to be calculated. It depends if you are using a listDataProvider or something else.