Search in sources :

Example 1 with PeopleGenerator

use of com.vaadin.flow.data.bean.PeopleGenerator in project flow-components by vaadin.

the class GridViewBasicPage method createCallBackDataProvider.

private void createCallBackDataProvider() {
    Grid<Person> grid = new Grid<>();
    /*
         * This Data Provider doesn't load all items into the memory right away.
         * Grid will request only the data that should be shown in its current
         * view "window". The Data Provider will use callbacks to load only a
         * portion of the data.
         */
    PeopleGenerator generator = new PeopleGenerator();
    grid.setItems(DataProvider.fromCallbacks(query -> IntStream.range(query.getOffset(), query.getOffset() + query.getLimit()).mapToObj(index -> generator.createPerson(index + 1)), query -> 100 * 1000 * 1000));
    grid.addColumn(Person::getFirstName).setHeader("Name");
    grid.addColumn(Person::getAge).setHeader("Age");
    grid.setId("lazy-loading");
    addCard("Grid with lazy loading", grid);
}
Also used : IntStream(java.util.stream.IntStream) PeopleGenerator(com.vaadin.flow.data.bean.PeopleGenerator) List(java.util.List) Person(com.vaadin.flow.data.bean.Person) Grid(com.vaadin.flow.component.grid.Grid) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) DataProvider(com.vaadin.flow.data.provider.DataProvider) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) Route(com.vaadin.flow.router.Route) Grid(com.vaadin.flow.component.grid.Grid) Person(com.vaadin.flow.data.bean.Person) PeopleGenerator(com.vaadin.flow.data.bean.PeopleGenerator)

Example 2 with PeopleGenerator

use of com.vaadin.flow.data.bean.PeopleGenerator 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)2 PeopleGenerator (com.vaadin.flow.data.bean.PeopleGenerator)2 Person (com.vaadin.flow.data.bean.Person)2 List (java.util.List)2 SelectionMode (com.vaadin.flow.component.grid.Grid.SelectionMode)1 GridContextMenu (com.vaadin.flow.component.grid.contextmenu.GridContextMenu)1 Div (com.vaadin.flow.component.html.Div)1 Hr (com.vaadin.flow.component.html.Hr)1 NativeButton (com.vaadin.flow.component.html.NativeButton)1 DataProvider (com.vaadin.flow.data.provider.DataProvider)1 NativeButtonRenderer (com.vaadin.flow.data.renderer.NativeButtonRenderer)1 Route (com.vaadin.flow.router.Route)1 IntStream (java.util.stream.IntStream)1