Search in sources :

Example 1 with GridContextMenu

use of com.vaadin.flow.component.grid.contextmenu.GridContextMenu in project flow-components by vaadin.

the class GridViewContextMenuPage method createContextSubMenu.

private void createContextSubMenu() {
    Grid<Person> grid = new Grid<>();
    ListDataProvider<Person> dataProvider = DataProvider.ofCollection(getItems());
    grid.setDataProvider(dataProvider);
    grid.addColumn(Person::getFirstName).setHeader("Name");
    grid.addColumn(Person::getAge).setHeader("Age");
    GridContextMenu<Person> contextMenu = new GridContextMenu<>(grid);
    GridMenuItem<Person> insert = contextMenu.addItem("Insert");
    insert.getSubMenu().addItem("Insert a row above", event -> {
        Optional<Person> item = event.getItem();
        if (!item.isPresent()) {
            // no selected row
            return;
        }
        List<Person> items = (List) dataProvider.getItems();
        items.add(items.indexOf(item.get()), new PeopleGenerator().createPerson(items.size() + 1));
        dataProvider.refreshAll();
    });
    insert.getSubMenu().add(new Hr());
    insert.getSubMenu().addItem("Insert a row below", event -> {
        Optional<Person> item = event.getItem();
        if (!item.isPresent()) {
            // no selected row
            return;
        }
        List<Person> items = (List) dataProvider.getItems();
        items.add(items.indexOf(item.get()) + 1, new PeopleGenerator().createPerson(items.size() + 1));
        dataProvider.refreshAll();
    });
    grid.setId("context-submenu-grid");
    addCard("Context Menu", "Using Context Sub Menu With Grid", grid, contextMenu);
}
Also used : Grid(com.vaadin.flow.component.grid.Grid) List(java.util.List) Hr(com.vaadin.flow.component.html.Hr) Person(com.vaadin.flow.data.bean.Person) GridContextMenu(com.vaadin.flow.component.grid.contextmenu.GridContextMenu) PeopleGenerator(com.vaadin.flow.data.bean.PeopleGenerator)

Aggregations

Grid (com.vaadin.flow.component.grid.Grid)1 GridContextMenu (com.vaadin.flow.component.grid.contextmenu.GridContextMenu)1 Hr (com.vaadin.flow.component.html.Hr)1 PeopleGenerator (com.vaadin.flow.data.bean.PeopleGenerator)1 Person (com.vaadin.flow.data.bean.Person)1 List (java.util.List)1