Search in sources :

Example 11 with ListBox

use of com.vaadin.flow.component.listbox.ListBox in project flow-components by vaadin.

the class ListBoxDataViewPage method createIdentifierProviderForListBox.

private void createIdentifierProviderForListBox() {
    CustomItem first = new CustomItem(1L, "First");
    CustomItem second = new CustomItem(2L, "Second");
    CustomItem third = new CustomItem(3L, "Third");
    List<CustomItem> items = new ArrayList<>(Arrays.asList(first, second, third));
    ListBox<CustomItem> listBox = new ListBox<>();
    ListBoxListDataView<CustomItem> listDataView = listBox.setItems(items);
    // Setting the following Identifier Provider makes the component
    // independent from the CustomItem's equals method implementation:
    listDataView.setIdentifierProvider(CustomItem::getId);
    listBox.setValue(new CustomItem(3L));
    Span selectedIdsSpan = new Span();
    selectedIdsSpan.setId(LIST_BOX_SELECTED_IDS_SPAN);
    selectedIdsSpan.setText(String.valueOf(listBox.getValue().getId()));
    Button updateAndSelectByIdOnlyButton = new Button("Update & Select by Id", click -> {
        // Make the names of unselected items similar to the name of
        // selected one to mess with the <equals> implementation in
        // CustomItem:
        first.setName("Second");
        listDataView.refreshItem(first);
        third.setName("Second");
        listDataView.refreshItem(third);
        // Select the item not with the reference of existing item,
        // and instead with just the Id:
        listBox.setValue(new CustomItem(2L));
        selectedIdsSpan.setText(String.valueOf(listBox.getValue().getId()));
    });
    updateAndSelectByIdOnlyButton.setId(LIST_BOX_SELECTION_BY_ID_UPDATE_BUTTON);
    Button selectByIdAndNameButton = new Button("Select by Id and Name", click -> {
        // Select the item with the Id and a challenging wrong name
        // to verify <equals> method is not in use:
        listBox.setValue(new CustomItem(3L, "Second"));
        selectedIdsSpan.setText(String.valueOf(listBox.getValue().getId()));
    });
    selectByIdAndNameButton.setId(LIST_BOX_SELECTION_BY_ID_AND_NAME_UPDATE_BUTTON);
    add(listBox, selectedIdsSpan, updateAndSelectByIdOnlyButton, selectByIdAndNameButton);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) Button(com.vaadin.flow.component.button.Button) ArrayList(java.util.ArrayList) MultiSelectListBox(com.vaadin.flow.component.listbox.MultiSelectListBox) ListBox(com.vaadin.flow.component.listbox.ListBox) Span(com.vaadin.flow.component.html.Span)

Example 12 with ListBox

use of com.vaadin.flow.component.listbox.ListBox in project flow-components by vaadin.

the class ListBoxViewDemoPage method addDisabledListBox.

private void addDisabledListBox() {
    Label message = new Label("-");
    message.setId("message-label");
    ListBox<String> listBox = new ListBox<>();
    listBox.setItems("Bread", "Butter", "Milk");
    listBox.setEnabled(false);
    listBox.addValueChangeListener(event -> message.setText(String.format("Selection changed from %s to %s, selection is from client: %s", event.getOldValue(), event.getValue(), event.isFromClient())));
    NativeButton button = new NativeButton("Select Milk", event -> listBox.setValue("Milk"));
    Label note = new Label("Note! Even though updating from the client doesn't work, " + "the server may push a new status for the component.");
    addCard("Disabled ListBox and selection", listBox, button, message, note).setId("disabled-list-box");
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) Label(com.vaadin.flow.component.html.Label) ListBox(com.vaadin.flow.component.listbox.ListBox)

Aggregations

ListBox (com.vaadin.flow.component.listbox.ListBox)12 NativeButton (com.vaadin.flow.component.html.NativeButton)11 MultiSelectListBox (com.vaadin.flow.component.listbox.MultiSelectListBox)8 ArrayList (java.util.ArrayList)5 Span (com.vaadin.flow.component.html.Span)4 Button (com.vaadin.flow.component.button.Button)3 Div (com.vaadin.flow.component.html.Div)3 Label (com.vaadin.flow.component.html.Label)3 ListBoxListDataView (com.vaadin.flow.component.listbox.dataview.ListBoxListDataView)3 Route (com.vaadin.flow.router.Route)3 List (java.util.List)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Stream (java.util.stream.Stream)3 H3 (com.vaadin.flow.component.html.H3)2 ListBoxDataView (com.vaadin.flow.component.listbox.dataview.ListBoxDataView)2 AbstractDataProvider (com.vaadin.flow.data.provider.AbstractDataProvider)2 DataProvider (com.vaadin.flow.data.provider.DataProvider)2 ListDataProvider (com.vaadin.flow.data.provider.ListDataProvider)2 Query (com.vaadin.flow.data.provider.Query)2 Arrays (java.util.Arrays)2