Search in sources :

Example 41 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer in project flow-components by vaadin.

the class MainView method createEditorColumns.

protected void createEditorColumns() {
    Div itemDisplayPanel = new Div();
    Div subPropertyDisplayPanel = new Div();
    subPropertyDisplayPanel.setId("prop-panel");
    Div eventsPanel = new Div();
    eventsPanel.setId("events-panel");
    GridPro<Person> grid = new GridPro<>();
    Button disableGrid = new Button("Disable Grid");
    disableGrid.setId("disable-grid-id");
    List<City> cityList = createCityItems();
    List<Person> personList = createItems();
    mapLists(personList, cityList);
    grid.setItems(personList);
    grid.addCellEditStartedListener(e -> eventsPanel.add(e.getItem().toString()));
    grid.addColumn(Person::getAge).setHeader("Age");
    grid.addEditColumn(Person::getName, "name").text((item, newValue) -> {
        item.setName(newValue);
        itemDisplayPanel.setText(item.toString());
        subPropertyDisplayPanel.setText(newValue);
    }).setHeader("Name").setWidth("300px");
    ComboBox<Department> cb = new ComboBox<>();
    cb.setItems(Department.values());
    grid.addEditColumn(Person::getDepartment).custom(cb, (item, newValue) -> {
        item.setDepartment(newValue);
        itemDisplayPanel.setText(item.toString());
        subPropertyDisplayPanel.setText(String.valueOf(newValue));
    }).setHeader("Department").setWidth("300px");
    ComponentRenderer<Span, Person> booleanRenderer = new ComponentRenderer<>(person -> new Span(person.isSubscriber() ? "Yes" : "No"));
    grid.addEditColumn(Person::isSubscriber, booleanRenderer).checkbox((item, newValue) -> {
        item.setSubscriber(newValue);
        itemDisplayPanel.setText(item.toString());
        subPropertyDisplayPanel.setText(newValue.toString());
    }).setHeader("Subscriber").setWidth("300px");
    ComboBox<City> cityCb = new ComboBox<>();
    cityCb.setItems(cityList);
    cityCb.setItemLabelGenerator(City::getName);
    ComponentRenderer<Span, Person> cityRenderer = new ComponentRenderer<>(person -> {
        if (person.getCity() != null) {
            return new Span(person.getCity().getName());
        } else {
            return new Span("");
        }
    });
    grid.addEditColumn(Person::getCity, cityRenderer).custom(cityCb, (item, newValue) -> {
        item.setCity(newValue);
        newValue.setPerson(item);
        itemDisplayPanel.setText(item.toString());
        subPropertyDisplayPanel.setText(newValue.toString());
    }).setHeader("City").setWidth("300px");
    Input customField = new Input();
    grid.addEditColumn(Person::getEmail).custom(customField, (item, newValue) -> item.setEmail(newValue)).setHeader("Email").setWidth("300px");
    disableGrid.addClickListener(click -> grid.setEnabled(false));
    add(grid, itemDisplayPanel, subPropertyDisplayPanel, eventsPanel, disableGrid);
}
Also used : ComboBox(com.vaadin.flow.component.combobox.ComboBox) Span(com.vaadin.flow.component.html.Span) Div(com.vaadin.flow.component.html.Div) Input(com.vaadin.flow.component.html.Input) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Button(com.vaadin.flow.component.button.Button) GridPro(com.vaadin.flow.component.gridpro.GridPro)

Example 42 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer in project flow-components by vaadin.

the class RadioButtonGroupDemoPage method addItemRenderer.

private void addItemRenderer() {
    Div message = new Div();
    RadioButtonGroup<Person> group = new RadioButtonGroup<>();
    group.setItems(new Person(1, "Joe"), new Person(2, "John"), new Person(3, "Bill"));
    group.setRenderer(new ComponentRenderer<>(person -> new Anchor("http://example.com/" + person.getId(), person.getName())));
    group.addValueChangeListener(event -> message.setText(String.format("Radio button group value changed from '%s' to '%s'", getName(event.getOldValue()), getName(event.getValue()))));
    group.setId("button-group-renderer");
    message.setId("button-group-renderer-value");
    addCard("Radio button group with renderer", group, message);
}
Also used : Div(com.vaadin.flow.component.html.Div) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Anchor(com.vaadin.flow.component.html.Anchor) Image(com.vaadin.flow.component.html.Image) Component(com.vaadin.flow.component.Component) RadioGroupVariant(com.vaadin.flow.component.radiobutton.RadioGroupVariant) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) H2(com.vaadin.flow.component.html.H2) Hr(com.vaadin.flow.component.html.Hr) RadioButtonGroup(com.vaadin.flow.component.radiobutton.RadioButtonGroup) IconRenderer(com.vaadin.flow.data.renderer.IconRenderer) Route(com.vaadin.flow.router.Route) Span(com.vaadin.flow.component.html.Span) Anchor(com.vaadin.flow.component.html.Anchor) RadioButtonGroup(com.vaadin.flow.component.radiobutton.RadioButtonGroup)

Example 43 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer in project flow-components by vaadin.

the class VirtualListPage method createListWithComponentRendererWithBeansAndPlaceholder.

private void createListWithComponentRendererWithBeansAndPlaceholder() {
    VirtualList<Person> list = new VirtualList<>();
    list.setHeight("100px");
    List<Person> people = createPeople(100);
    list.setRenderer(new ComponentRenderer<Div, Person>(person -> {
        Div text = new Div(new Text(person.getName()));
        text.addClassName("component-rendered");
        return text;
    }));
    list.setItems(people);
    list.setId("component-renderer-with-beans");
    Person placeholder = new Person();
    placeholder.setName("the-placeholder");
    list.setPlaceholderItem(placeholder);
    add(list);
}
Also used : Div(com.vaadin.flow.component.html.Div) IntStream(java.util.stream.IntStream) Text(com.vaadin.flow.component.Text) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) LocalDate(java.time.LocalDate) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) HasComponents(com.vaadin.flow.component.HasComponents) Renderer(com.vaadin.flow.data.renderer.Renderer) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) Text(com.vaadin.flow.component.Text)

Example 44 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer in project flow-components by vaadin.

the class GridBenchmark method setParameter.

@Override
public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
    Location location = event.getLocation();
    QueryParameters queryParameters = location.getQueryParameters();
    Map<String, List<String>> parametersMap = queryParameters.getParameters();
    if (!parametersMap.containsKey("variant") || !parametersMap.containsKey("metric")) {
        add(new Text("Provide query parameters: variant and metric"));
        return;
    }
    String metric = parametersMap.get("metric").get(0);
    String variant = parametersMap.get("variant").get(0);
    LoggerFactory.getLogger(GridBenchmark.class).info("Sample: " + variant + "-" + metric);
    switch(variant) {
        case "simple":
            grid = getGrid();
            addColumns(grid, 5, false);
            break;
        case "multicolumn":
            grid = getGrid();
            addColumns(grid, 50, false);
            break;
        case "componentrenderers":
            grid = getGrid();
            addColumns(grid, 5, true);
            break;
        case "detailsopened":
            grid = getGrid();
            addColumns(grid, 5, false);
            grid.setItemDetailsRenderer(new ComponentRenderer<>(item -> new Text(item.toString())));
            items.forEach(item -> grid.setDetailsVisible(item, true));
            break;
        case "tree":
            grid = getTreeGrid();
            ((TreeGrid<String>) grid).addHierarchyColumn(i -> i);
            addColumns(grid, 5, false);
            break;
        case "mixed":
            grid = getTreeGrid();
            ((TreeGrid<String>) grid).addHierarchyColumn(i -> i);
            addColumns(grid, 50, true);
            grid.setItemDetailsRenderer(new ComponentRenderer<>(item -> new Text(item.toString())));
            treeData.getRootItems().forEach(item -> grid.setDetailsVisible(item, true));
            break;
        default:
            break;
    }
    switch(metric) {
        case "verticalscrollframetime":
            add(grid);
            whenRendered(grid).then(v -> grid.getElement().executeJs("window.measureScrollFrameTime(this, false)"));
            break;
        case "horizontalscrollframetime":
            add(grid);
            whenRendered(grid).then(v -> grid.getElement().executeJs("window.measureScrollFrameTime(this, true)"));
            break;
        case "rendertime":
            measureRendered(grid);
            UI.getCurrent().getElement().executeJs("return window.startWhenReady()").then(v -> add(grid));
            break;
        case "expandtime":
            add(grid);
            startWhenRendered(grid).then(v -> {
                measureRendered(grid);
                TreeGrid<String> treeGrid = (TreeGrid<String>) grid;
                TreeData<String> data = ((TreeDataProvider<String>) treeGrid.getDataProvider()).getTreeData();
                treeGrid.expandRecursively(data.getRootItems(), 5);
            });
            break;
        default:
            break;
    }
}
Also used : IntStream(java.util.stream.IntStream) Text(com.vaadin.flow.component.Text) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Grid(com.vaadin.flow.component.grid.Grid) HasUrlParameter(com.vaadin.flow.router.HasUrlParameter) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData) LoggerFactory(org.slf4j.LoggerFactory) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) BeforeEvent(com.vaadin.flow.router.BeforeEvent) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) TreeDataProvider(com.vaadin.flow.data.provider.hierarchy.TreeDataProvider) Route(com.vaadin.flow.router.Route) OptionalParameter(com.vaadin.flow.router.OptionalParameter) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) List(java.util.List) Map(java.util.Map) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) Collections(java.util.Collections) JsModule(com.vaadin.flow.component.dependency.JsModule) QueryParameters(com.vaadin.flow.router.QueryParameters) Text(com.vaadin.flow.component.Text) QueryParameters(com.vaadin.flow.router.QueryParameters) TreeDataProvider(com.vaadin.flow.data.provider.hierarchy.TreeDataProvider) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) List(java.util.List) Location(com.vaadin.flow.router.Location)

Example 45 with ComponentRenderer

use of com.vaadin.flow.data.renderer.ComponentRenderer in project flow-components by vaadin.

the class GridWithTemplatePage method createStandaloneGridWithTemplatesInTheCells.

private void createStandaloneGridWithTemplatesInTheCells() {
    Grid<String> grid = new Grid<>();
    setCommonGridFeatures(grid, "standalone-template-in-cells");
    grid.addColumn(value -> value);
    grid.addColumn(new ComponentRenderer<>(value -> getTestTemplate(value, grid.getId().get())));
    add(new H3("Grid with templates in the cells"), grid);
}
Also used : ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Grid(com.vaadin.flow.component.grid.Grid) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) Element(com.vaadin.flow.dom.Element) Div(com.vaadin.flow.component.html.Div) H3(com.vaadin.flow.component.html.H3) H2(com.vaadin.flow.component.html.H2) Route(com.vaadin.flow.router.Route) Grid(com.vaadin.flow.component.grid.Grid) H3(com.vaadin.flow.component.html.H3)

Aggregations

ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)49 Div (com.vaadin.flow.component.html.Div)33 Route (com.vaadin.flow.router.Route)30 Grid (com.vaadin.flow.component.grid.Grid)23 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)20 List (java.util.List)19 Icon (com.vaadin.flow.component.icon.Icon)18 NativeButton (com.vaadin.flow.component.html.NativeButton)14 VaadinIcon (com.vaadin.flow.component.icon.VaadinIcon)14 TemplateRenderer (com.vaadin.flow.data.renderer.TemplateRenderer)14 Collectors (java.util.stream.Collectors)14 Label (com.vaadin.flow.component.html.Label)13 Span (com.vaadin.flow.component.html.Span)12 EnhancedButton (org.komunumo.ui.component.EnhancedButton)12 Component (com.vaadin.flow.component.Component)10 H2 (com.vaadin.flow.component.html.H2)10 ArrayList (java.util.ArrayList)10 Element (com.vaadin.flow.dom.Element)9 Collections (java.util.Collections)9 UI (com.vaadin.flow.component.UI)8