Search in sources :

Example 6 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxView method filteringAndSortingWithDataView.

private void filteringAndSortingWithDataView() {
    // PersonService can be found:
    // https://github.com/vaadin/vaadin-combo-box-flow/tree/master/vaadin-combo-box-flow-demo/src/main/java/com/vaadin/flow/component/combobox/demo/service/PersonService.java
    ComboBox<Person> comboBox = new ComboBox<>("Persons");
    PersonService personService = new PersonService();
    // We fetch the items to the memory and bind the obtained collection
    // to the combo box
    Collection<Person> persons = personService.fetchAll();
    ComboBoxListDataView<Person> dataView = comboBox.setItems(persons);
    /*
         * Providing a predicate item filter allows filtering by any field of
         * the business entity and apply a combo box's text filter independently
         */
    IntegerField personAgeFilter = new IntegerField(event -> dataView.setFilter(person -> event.getValue() == null || person.getAge() > event.getValue()));
    /*
         * Providing a value provider or comparator allows sorting combo box's
         * items by custom field, or combination of fields
         */
    Button sortPersons = new Button("Sort Persons by Name", event -> dataView.setSortOrder(Person::toString, SortDirection.ASCENDING));
    personAgeFilter.setLabel("Filter Persons with age more than:");
    personAgeFilter.setWidth(WIDTH_STRING);
    addCard("Filtering", "Filtering and Sorting with Data View", comboBox, personAgeFilter, sortPersons);
}
Also used : ItemFilter(com.vaadin.flow.component.combobox.ComboBox.ItemFilter) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Person(com.vaadin.flow.component.combobox.test.entity.Person) Image(com.vaadin.flow.component.html.Image) SortDirection(com.vaadin.flow.data.provider.SortDirection) Component(com.vaadin.flow.component.Component) ProjectData(com.vaadin.flow.component.combobox.test.data.ProjectData) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) ElementData(com.vaadin.flow.component.combobox.test.data.ElementData) Div(com.vaadin.flow.component.html.Div) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Song(com.vaadin.flow.component.combobox.test.entity.Song) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) FlexComponent(com.vaadin.flow.component.orderedlayout.FlexComponent) FlexLayout(com.vaadin.flow.component.orderedlayout.FlexLayout) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) IntegerField(com.vaadin.flow.component.textfield.IntegerField) ElementConstants(com.vaadin.flow.dom.ElementConstants) Department(com.vaadin.flow.component.combobox.test.entity.Department) Paragraph(com.vaadin.flow.component.html.Paragraph) Notification(com.vaadin.flow.component.notification.Notification) Ticket(com.vaadin.flow.component.combobox.test.entity.Ticket) Project(com.vaadin.flow.component.combobox.test.entity.Project) Anchor(com.vaadin.flow.component.html.Anchor) Query(com.vaadin.flow.data.provider.Query) ComboBoxLazyDataView(com.vaadin.flow.component.combobox.dataview.ComboBoxLazyDataView) Collection(java.util.Collection) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) PersonService(com.vaadin.flow.component.combobox.test.service.PersonService) H2(com.vaadin.flow.component.html.H2) Element(com.vaadin.flow.component.combobox.test.entity.Element) DepartmentData(com.vaadin.flow.component.combobox.test.data.DepartmentData) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Stream(java.util.stream.Stream) ComboBoxListDataView(com.vaadin.flow.component.combobox.dataview.ComboBoxListDataView) Span(com.vaadin.flow.component.html.Span) Button(com.vaadin.flow.component.button.Button) ComboBox(com.vaadin.flow.component.combobox.ComboBox) PersonService(com.vaadin.flow.component.combobox.test.service.PersonService) IntegerField(com.vaadin.flow.component.textfield.IntegerField) Person(com.vaadin.flow.component.combobox.test.entity.Person)

Example 7 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxView method themeVariantsTextAlign.

private void themeVariantsTextAlign() {
    Div div = new Div();
    ComboBox<String> leftComboBox = new ComboBox<>();
    leftComboBox.setItems("Left", "Center", "Right");
    leftComboBox.setValue("Left");
    leftComboBox.getElement().setAttribute("theme", "align-left");
    ComboBox<String> centerComboBox = new ComboBox<>();
    centerComboBox.setItems("Left", "Center", "Right");
    centerComboBox.setValue("Center");
    centerComboBox.getElement().setAttribute("theme", "align-center");
    ComboBox<String> rightComboBox = new ComboBox<>();
    rightComboBox.setItems("Left", "Center", "Right");
    rightComboBox.setValue("Right");
    rightComboBox.getElement().setAttribute("theme", "align-right");
    div.add(leftComboBox, centerComboBox, rightComboBox);
    leftComboBox.getStyle().set("margin-right", "5px");
    centerComboBox.getStyle().set("margin-right", "5px");
    addCard("Theme Variants", "Text align", div);
}
Also used : Div(com.vaadin.flow.component.html.Div) ComboBox(com.vaadin.flow.component.combobox.ComboBox)

Example 8 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxView method storingCustomValues.

private void storingCustomValues() {
    Div message = createMessageDiv("custom-value-message");
    ComboBox<Project> comboBox = new ComboBox<>("Project");
    comboBox.setItems(this::fetchProjects, this::countProjects);
    comboBox.setItemLabelGenerator(Project::getName);
    comboBox.addValueChangeListener(valueChangeEvent -> {
        if (valueChangeEvent.getValue() == null) {
            message.setText("No project selected");
        } else {
            message.setText("Selected value: " + valueChangeEvent.getValue());
        }
    });
    comboBox.addCustomValueSetListener(event -> {
        Project project = projectData.addProject(event.getDetail());
        comboBox.setValue(project);
    });
    addCard("Storing custom values", comboBox, message);
}
Also used : Div(com.vaadin.flow.component.html.Div) Project(com.vaadin.flow.component.combobox.test.entity.Project) ComboBox(com.vaadin.flow.component.combobox.ComboBox)

Example 9 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxView method helperText.

private void helperText() {
    Div div = new Div();
    ComboBox<String> helperTextCombobox = new ComboBox<>("Language");
    helperTextCombobox.setItems("Java", "Python", "C++", "Scala", "JavaScript");
    helperTextCombobox.setHelperText("Select the language you are most familiar with");
    ComboBox<String> helperComponentCombobox = new ComboBox<>("Continent");
    helperComponentCombobox.setItems("North America", "South America", "Africa", "Europe", "Asia", "Australia", "Antarctica");
    helperComponentCombobox.setHelperComponent(new Span("Select the continent of your residence"));
    add(helperTextCombobox, helperComponentCombobox);
    helperTextCombobox.getStyle().set("margin-right", "15px");
    div.add(helperTextCombobox, helperComponentCombobox);
    addCard("Helper text and helper component", div);
}
Also used : Div(com.vaadin.flow.component.html.Div) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Span(com.vaadin.flow.component.html.Span)

Example 10 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class DataProviderPage method createRefreshWithSmallerDataSet.

private void createRefreshWithSmallerDataSet() {
    add(new Hr());
    ComboBox<String> cb = new ComboBox<>();
    cb.setId("combo-box-with-reduce-data-set");
    Span cbWrapper = new Span(cb);
    List<String> items = new ArrayList<>();
    items.add("foo");
    items.add("bar");
    DataProvider<String, String> dp = DataProvider.fromFilteringCallbacks(q -> items.stream().skip(q.getOffset()).limit(q.getLimit()), q -> items.size());
    cb.setDataProvider(dp);
    NativeButton refreshAllWithSmallerDataSetButton = new NativeButton("Refresh all with smaller data set", event -> {
        items.remove("foo");
        dp.refreshAll();
    });
    refreshAllWithSmallerDataSetButton.setId("refresh-all-with-smaller-data-set");
    NativeButton toggleAttachedButton = new NativeButton("Toggle attached", event -> {
        if (cb.isAttached()) {
            cbWrapper.remove(cb);
        } else {
            cbWrapper.add(cb);
        }
    });
    toggleAttachedButton.setId("toggle-attached");
    add(cbWrapper, refreshAllWithSmallerDataSetButton, toggleAttachedButton);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) ComboBox(com.vaadin.flow.component.combobox.ComboBox) ArrayList(java.util.ArrayList) Hr(com.vaadin.flow.component.html.Hr) Span(com.vaadin.flow.component.html.Span)

Aggregations

ComboBox (com.vaadin.flow.component.combobox.ComboBox)69 Div (com.vaadin.flow.component.html.Div)35 Span (com.vaadin.flow.component.html.Span)21 NativeButton (com.vaadin.flow.component.html.NativeButton)20 Route (com.vaadin.flow.router.Route)18 List (java.util.List)16 H2 (com.vaadin.flow.component.html.H2)14 Collectors (java.util.stream.Collectors)14 Label (com.vaadin.flow.component.html.Label)12 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)12 Stream (java.util.stream.Stream)12 Component (com.vaadin.flow.component.Component)10 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)10 ArrayList (java.util.ArrayList)10 Button (com.vaadin.flow.component.button.Button)9 Person (com.vaadin.flow.component.combobox.test.entity.Person)9 PersonService (com.vaadin.flow.component.combobox.test.service.PersonService)9 IntStream (java.util.stream.IntStream)9 ItemFilter (com.vaadin.flow.component.combobox.ComboBox.ItemFilter)8 Paragraph (com.vaadin.flow.component.html.Paragraph)8