Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alejandro-du/3b417f3c5c83bf15571c59f0158c1672 to your computer and use it in GitHub Desktop.
Save alejandro-du/3b417f3c5c83bf15571c59f0158c1672 to your computer and use it in GitHub Desktop.
Vaadin 8.1 - Grid Drag and Drop example
package com.example;
import com.vaadin.shared.ui.grid.DropMode;
import com.vaadin.ui.Grid;
import com.vaadin.ui.GridDragSource;
import com.vaadin.ui.GridDropTarget;
import com.vaadin.ui.VerticalLayout;
import java.util.List;
/**
* @author Alejandro Duarte.
*/
public class GridDragAndDrop extends VerticalLayout {
private Service service = new Service();
private List<Person> people = service.findAll();
private Person dragged = null;
public GridDragAndDrop() {
Grid<Person> grid = new Grid<>(Person.class);
grid.setWidth("360px");
grid.setHeight("300px");
grid.setColumns("id", "firstName");
grid.getColumn("id").setWidth(100);
grid.setItems(people);
GridDragSource<Person> source = new GridDragSource<>(grid);
source.addGridDragStartListener(e -> {
dragged = e.getDraggedItems().iterator().next();
});
GridDropTarget<Person> target = new GridDropTarget<>(grid, DropMode.ON_TOP_OR_BETWEEN);
target.addGridDropListener(e -> {
int index = people.indexOf(e.getDropTargetRow());
people.remove(dragged);
people.add(index, dragged);
grid.getDataProvider().refreshAll();
});
addComponent(grid);
}
}
@mstahv
Copy link

mstahv commented May 11, 2017

@Legioth would it be hard to add a shorthand for single select mode to return just a single row?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment