Search in sources :

Example 41 with TextField

use of com.vaadin.flow.component.textfield.TextField in project flow-components by vaadin.

the class GridViewEditorPage method createNotBufferedEditor.

private void createNotBufferedEditor() {
    Div message = new Div();
    message.setId("not-buffered-editor-msg");
    Grid<Person> grid = new Grid<>();
    List<Person> persons = getItems();
    grid.setItems(persons);
    Column<Person> nameColumn = grid.addColumn(Person::getFirstName).setHeader("Name");
    Column<Person> subscriberColumn = grid.addColumn(Person::isSubscriber).setHeader("Subscriber");
    Binder<Person> binder = new Binder<>(Person.class);
    grid.getEditor().setBinder(binder);
    TextField field = new TextField();
    // Close the editor in case of backward between components
    field.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && event.shiftKey");
    binder.bind(field, "firstName");
    nameColumn.setEditorComponent(field);
    Checkbox checkbox = new Checkbox();
    binder.bind(checkbox, "subscriber");
    subscriberColumn.setEditorComponent(checkbox);
    // Close the editor in case of forward navigation between
    checkbox.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && !event.shiftKey");
    grid.addItemDoubleClickListener(event -> {
        grid.getEditor().editItem(event.getItem());
        field.focus();
    });
    grid.addItemClickListener(event -> {
        if (binder.getBean() != null) {
            message.setText(binder.getBean().getFirstName() + ", " + binder.getBean().isSubscriber());
        }
    });
    grid.setId("not-buffered-editor");
    addCard("Grid Editor", "Editor in Not Buffered Mode", message, grid);
}
Also used : Div(com.vaadin.flow.component.html.Div) Grid(com.vaadin.flow.component.grid.Grid) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Collection(java.util.Collection) Editor(com.vaadin.flow.component.grid.editor.Editor) Binder(com.vaadin.flow.data.binder.Binder) Div(com.vaadin.flow.component.html.Div) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Column(com.vaadin.flow.component.grid.Grid.Column) Person(com.vaadin.flow.data.bean.Person) Collections(java.util.Collections) TextField(com.vaadin.flow.component.textfield.TextField) WeakHashMap(java.util.WeakHashMap) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Example 42 with TextField

use of com.vaadin.flow.component.textfield.TextField in project flow-components by vaadin.

the class GridViewFilteringPage method createGridWithFilters.

private void createGridWithFilters() {
    Grid<Person> grid = new Grid<>();
    ListDataProvider<Person> dataProvider = new ListDataProvider<>(createItems());
    grid.setDataProvider(dataProvider);
    List<ValueProvider<Person, String>> valueProviders = new ArrayList<>();
    valueProviders.add(Person::getFirstName);
    valueProviders.add(person -> String.valueOf(person.getAge()));
    valueProviders.add(person -> person.getAddress().getStreet());
    valueProviders.add(person -> String.valueOf(person.getAddress().getPostalCode()));
    Iterator<ValueProvider<Person, String>> iterator = valueProviders.iterator();
    grid.addColumn(iterator.next()).setHeader("Name");
    grid.addColumn(iterator.next()).setHeader("Age");
    grid.addColumn(iterator.next()).setHeader("Street");
    grid.addColumn(iterator.next()).setHeader("Postal Code");
    HeaderRow filterRow = grid.appendHeaderRow();
    Iterator<ValueProvider<Person, String>> iterator2 = valueProviders.iterator();
    grid.getColumns().forEach(column -> {
        TextField field = new TextField();
        ValueProvider<Person, String> valueProvider = iterator2.next();
        field.addValueChangeListener(event -> dataProvider.addFilter(person -> StringUtils.containsIgnoreCase(valueProvider.apply(person), field.getValue())));
        field.setValueChangeMode(ValueChangeMode.EAGER);
        filterRow.getCell(column).setComponent(field);
        field.setSizeFull();
        field.setPlaceholder("Filter");
    });
    grid.setId("grid-with-filters");
    addCard("Filtering", "Using text fields for filtering items", grid);
}
Also used : ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) List(java.util.List) Person(com.vaadin.flow.data.bean.Person) Grid(com.vaadin.flow.component.grid.Grid) Iterator(java.util.Iterator) HeaderRow(com.vaadin.flow.component.grid.HeaderRow) ValueProvider(com.vaadin.flow.function.ValueProvider) StringUtils(org.apache.commons.lang3.StringUtils) TextField(com.vaadin.flow.component.textfield.TextField) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Grid(com.vaadin.flow.component.grid.Grid) ArrayList(java.util.ArrayList) HeaderRow(com.vaadin.flow.component.grid.HeaderRow) TextField(com.vaadin.flow.component.textfield.TextField) ValueProvider(com.vaadin.flow.function.ValueProvider) Person(com.vaadin.flow.data.bean.Person)

Example 43 with TextField

use of com.vaadin.flow.component.textfield.TextField in project flow-components by vaadin.

the class GridViewUsingComponentsPage method createColumnComponentRenderer.

private void createColumnComponentRenderer() {
    Grid<Person> grid = new Grid<>();
    grid.setItems(createItems());
    // Use the component constructor that accepts an item ->
    // new PersonComponent(Person person)
    grid.addComponentColumn(PersonComponent::new).setHeader("Person");
    // Or you can use an ordinary function to setup the component
    grid.addComponentColumn(item -> new NativeButton("Remove", evt -> {
        ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
        dataProvider.getItems().remove(item);
        dataProvider.refreshAll();
    })).setHeader("Actions");
    // Item details can also use components
    grid.setItemDetailsRenderer(new ComponentRenderer<>(PersonCard::new));
    // When items are updated, new components are generated
    TextField idField = new TextField("", "Person id");
    TextField nameField = new TextField("", "New name");
    NativeButton updateButton = new NativeButton("Update person", event -> {
        String id = idField.getValue();
        String name = nameField.getValue();
        ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
        dataProvider.getItems().stream().filter(person -> String.valueOf(person.getId()).equals(id)).findFirst().ifPresent(person -> {
            person.setFirstName(name);
            dataProvider.refreshItem(person);
        });
    });
    grid.setSelectionMode(SelectionMode.NONE);
    grid.setId("component-renderer");
    idField.setId("component-renderer-id-field");
    nameField.setId("component-renderer-name-field");
    updateButton.setId("component-renderer-update-button");
    addCard("Using components", "Grid with columns using component renderer", grid, idField, nameField, updateButton);
}
Also used : ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Person(com.vaadin.flow.data.bean.Person) Grid(com.vaadin.flow.component.grid.Grid) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) TextField(com.vaadin.flow.component.textfield.TextField) Span(com.vaadin.flow.component.html.Span) Route(com.vaadin.flow.router.Route) NativeButton(com.vaadin.flow.component.html.NativeButton) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Example 44 with TextField

use of com.vaadin.flow.component.textfield.TextField in project flow-components by vaadin.

the class ItemClickListenerPage method createGridWithChangingKeysOfItemsAndFocusableHeader.

private void createGridWithChangingKeysOfItemsAndFocusableHeader() {
    Span message = new Span();
    message.setId("item-click-event-log");
    ListDataProvider<String> dataProvider = new ListDataProvider<>(Arrays.asList("a", "b", "c"));
    Grid<String> grid = new Grid<>();
    grid.setItems(dataProvider);
    grid.addColumn(x -> x).setHeader("Header");
    grid.addItemClickListener(ev -> message.setText("ItemClicked"));
    grid.setId(GRID_FILTER_FOCUSABLE_HEADER);
    Button filterButton = new Button("Filter");
    filterButton.setId("filterButton");
    Button clearFilterButton = new Button("Clear filter");
    clearFilterButton.setId("clearFilterButton");
    filterButton.addClickListener(event -> filterGrid(dataProvider, "b"));
    clearFilterButton.addClickListener(event -> filterGrid(dataProvider, null));
    TextField focusableHeader = new TextField();
    focusableHeader.setId("focusableHeader");
    grid.prependHeaderRow().getCells().get(0).setComponent(focusableHeader);
    add(grid, filterButton, clearFilterButton, message);
}
Also used : ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Button(com.vaadin.flow.component.button.Button) SerializableFunction(com.vaadin.flow.function.SerializableFunction) Arrays(java.util.Arrays) Grid(com.vaadin.flow.component.grid.Grid) Div(com.vaadin.flow.component.html.Div) TextField(com.vaadin.flow.component.textfield.TextField) StringUtils(org.apache.commons.lang3.StringUtils) Span(com.vaadin.flow.component.html.Span) Route(com.vaadin.flow.router.Route) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Button(com.vaadin.flow.component.button.Button) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) Span(com.vaadin.flow.component.html.Span)

Example 45 with TextField

use of com.vaadin.flow.component.textfield.TextField in project flow-components by vaadin.

the class TreeComponentColumnsPage method addTreeGrid.

private void addTreeGrid(boolean addGridBefore) {
    TreeGrid<String> grid = new TreeGrid<>();
    if (addGridBefore) {
        grid.setId("grid-then-comp");
        add(grid);
    }
    ComponentRenderer<TextField, String> componentRenderer = new ComponentRenderer<>(TextField::new, (component, item) -> {
        component.setReadOnly(true);
        component.setValue(item);
    });
    grid.addComponentHierarchyColumn(this::createTextField).setHeader("Header A").setId("textfield");
    grid.addColumn(componentRenderer).setHeader("Header B");
    ComponentRenderer<Button, String> componentRendererBtn = new ComponentRenderer<>(() -> new Button("btn"), ((button, s) -> {
        button.setText(s);
        button.setThemeName(ButtonVariant.LUMO_ERROR.getVariantName());
    }));
    grid.addColumn(componentRendererBtn).setHeader("Header C");
    TreeData<String> data = new TreeData<>();
    final Map<String, String> parentPathMap = new HashMap<>();
    addRootItems("Granddad", 3, data, parentPathMap).forEach(granddad -> addItems("Dad", 3, granddad, data, parentPathMap).forEach(dad -> addItems("Son", 100, dad, data, parentPathMap)));
    grid.setDataProvider(new TreeDataProvider<>(data));
    if (!addGridBefore) {
        grid.setId("comp-then-grid");
        add(grid);
    }
}
Also used : ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) TreeGridHugeTreePage.addItems(com.vaadin.flow.component.treegrid.it.TreeGridHugeTreePage.addItems) ButtonVariant(com.vaadin.flow.component.button.ButtonVariant) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData) TreeGridHugeTreePage.addRootItems(com.vaadin.flow.component.treegrid.it.TreeGridHugeTreePage.addRootItems) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) HashMap(java.util.HashMap) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) TreeDataProvider(com.vaadin.flow.data.provider.hierarchy.TreeDataProvider) Route(com.vaadin.flow.router.Route) Button(com.vaadin.flow.component.button.Button) Map(java.util.Map) Command(com.vaadin.flow.server.Command) TextField(com.vaadin.flow.component.textfield.TextField) HashMap(java.util.HashMap) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) NativeButton(com.vaadin.flow.component.html.NativeButton) Button(com.vaadin.flow.component.button.Button) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) TextField(com.vaadin.flow.component.textfield.TextField) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData)

Aggregations

TextField (com.vaadin.flow.component.textfield.TextField)227 Test (org.junit.jupiter.api.Test)61 Button (com.vaadin.flow.component.button.Button)54 Div (com.vaadin.flow.component.html.Div)38 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)32 FormLayout (com.vaadin.flow.component.formlayout.FormLayout)29 Binder (com.vaadin.flow.data.binder.Binder)29 GeneratedVaadinTextField (com.vaadin.flow.component.textfield.GeneratedVaadinTextField)28 Element (com.vaadin.flow.dom.Element)26 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)23 Route (com.vaadin.flow.router.Route)21 Component (com.vaadin.flow.component.Component)16 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)15 BinderCrudEditor (com.vaadin.flow.component.crud.BinderCrudEditor)15 Grid (com.vaadin.flow.component.grid.Grid)15 Span (com.vaadin.flow.component.html.Span)15 List (java.util.List)15 EmailField (com.vaadin.flow.component.textfield.EmailField)14 TextArea (com.vaadin.flow.component.textfield.TextArea)14 Test (org.junit.Test)14