Class MultiSelectComboBox<TItem>
- Type Parameters:
TItem- the type of the items to be selectable from the combo box
- All Implemented Interfaces:
AttachNotifier,BlurNotifier<MultiSelectComboBox<TItem>>,DetachNotifier,Focusable<MultiSelectComboBox<TItem>>,FocusNotifier<MultiSelectComboBox<TItem>>,HasAriaLabel,HasElement,HasEnabled,HasHelper,HasLabel,HasPlaceholder,HasSize,HasStyle,HasTheme,HasValidation,HasValue<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,,Set<TItem>>, Set<TItem>> HasValueAndElement<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,,Set<TItem>>, Set<TItem>> HasAllowedCharPattern,HasAutoOpen,HasClearButton,HasThemeVariant<MultiSelectComboBoxVariant>,HasTooltip,HasValidationProperties,InputField<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,,Set<TItem>>, Set<TItem>> HasValidator<Set<TItem>>,HasDataView<TItem,,String, ComboBoxDataView<TItem>> HasLazyDataView<TItem,,String, ComboBoxLazyDataView<TItem>> HasListDataView<TItem,,ComboBoxListDataView<TItem>> MultiSelect<MultiSelectComboBox<TItem>,,TItem> Serializable
ComboBox, MultiSelectComboBox allows to select multiple values.
MultiSelectComboBox supports lazy loading. This means that when using large
data sets, items are requested from the server one "page" at a time when the
user scrolls down the overlay. The number of items in one page is by default
50, and can be changed with ComboBoxBase.setPageSize(int).
MultiSelectComboBox can do filtering either in the browser or in the server.
When MultiSelectComboBox has only a relatively small set of items, the
filtering will happen in the browser, allowing smooth user-experience. When
the size of the data set is larger than the pageSize, the
webcomponent doesn't necessarily have all the data available, and it will
make requests to the server to handle the filtering. Also, if you have
defined custom filtering logic, with eg.
ComboBoxBase.setItems(ComboBox.ItemFilter, Collection), filtering will happen in
the server. To enable client-side filtering with larger data sets, you can
override the pageSize to be bigger than the size of your data set.
However, then the full data set will be sent to the client immediately, and
you will lose the benefits of lazy loading.
Validation
MultiSelectComboBox comes with a built-in validation mechanism that verifies
that the field is not empty when required is enabled.
Validation is triggered whenever the user initiates a value change, for example by selection from the dropdown or manual entry followed by Enter or blur. Programmatic value changes trigger validation as well. If validation fails, the component is marked as invalid and an error message is displayed below the input.
The required error message can be configured using either
MultiSelectComboBoxI18n.setRequiredErrorMessage(String) or
HasValidationProperties.setErrorMessage(String).
For more advanced validation that requires custom rules, you can use
Binder. Please note that Binder provides its own API for the required
validation, see asRequired().
However, if Binder doesn't fit your needs and you want to implement fully
custom validation logic, you can disable the built-in validation by setting
ComboBoxBase.setManualValidation(boolean) to true. This will allow you to control
the invalid state and the error message manually using
HasValidationProperties.setInvalid(boolean) and HasValidationProperties.setErrorMessage(String) API.
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDefines possible behavior of the component when not all selected items can be displayed as chips within the current field width.Nested classes/interfaces inherited from class com.vaadin.flow.component.combobox.ComboBoxBase
ComboBoxBase.CustomValueSetEvent<TComponent extends ComboBoxBase<TComponent,?, ?>>, ComboBoxBase.SpringData Nested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
AbstractField.ComponentValueChangeEvent<C extends Component,V> Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier
BlurNotifier.BlurEvent<C extends Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier
FocusNotifier.FocusEvent<C extends Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>> -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.MultiSelectComboBox(int pageSize) Creates an empty combo box with the defined page size for lazy loading.MultiSelectComboBox(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener) Constructs a combo box with a value change listener.MultiSelectComboBox(String label) Creates an empty combo box with the defined label.MultiSelectComboBox(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener) Constructs a combo box with the defined label and a value change listener.MultiSelectComboBox(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener, TItem... items) Constructs a combo box with the defined label, a value change listener and populated with the items in the array.MultiSelectComboBox(String label, Collection<TItem> items) Creates a combo box with the defined label and populated with the items in the collection.MultiSelectComboBox(String label, TItem... items) Creates a combo box with the defined label and populated with the items in the array. -
Method Summary
Modifier and TypeMethodDescriptionAdds a selection listener that will be called when the selection is changed either by the user or programmatically.Gets the behavior of the component when not all selected items can be displayed as chips within the current field width.getI18n()Gets the internationalization object previously set for this component.Returns an immutable set of the currently selected items.getValue()Gets the value of the component, which is a set of selected items.booleanGets whether the filter is kept after selecting items.booleanGets whether the user is required to provide a value.booleanisSelected(TItem item) Whether the item is currently selected in the combo box.booleanGets whether selected items are grouped at the top of the overlay.protected voidonAttach(AttachEvent attachEvent) Called when the component is attached to a UI.protected voidRefresh value / selection of the web component after changes that might affect the presentation / rendering of itemsvoidsetAutoExpand(MultiSelectComboBox.AutoExpandMode autoExpandMode) Sets the behavior of the component when not all selected items can be displayed as chips within the current field width.voidSets the internationalization object for this component.voidsetKeepFilter(boolean keepFilter) Enables or disables keeping the filter after selecting items.voidsetOverlayWidth(float width, Unit unit) Sets the dropdown overlay width.voidsetOverlayWidth(String width) Sets the dropdown overlay width.voidsetRequiredIndicatorVisible(boolean required) Sets whether the user is required to provide a value.voidsetSelectedItemsOnTop(boolean selectedItemsOnTop) Enables or disables grouping of the selected items at the top of the overlay.voidsetValue(Collection<TItem> items) Sets the value of the component, which is a set of selected items.voidSets the value of the component, which is a set of selected items.final voidSets the value of the component, which is a set of selected items.voidupdateSelection(Set<TItem> addedItems, Set<TItem> removedItems) Updates the selection by adding and removing the given items from it.Methods inherited from class com.vaadin.flow.component.combobox.ComboBoxBase
addCustomValueSetListener, generateClassName, generateLabel, getAriaLabel, getAriaLabelledBy, getDataCommunicator, getDataController, getDataGenerator, getDataProvider, getDefaultValidator, getFilter, getGenericDataView, getItemClassNameGenerator, getItemLabelGenerator, getKeyMapper, getLazyDataView, getListDataView, getPageSize, getRenderManager, isAllowCustomValue, isAutofocus, isOpened, isRequired, onDetach, runBeforeClientResponse, setAllowCustomValue, setAriaLabel, setAriaLabelledBy, setAutofocus, setClassNameGenerator, setDataProvider, setDataProvider, setFilter, setI18n, setItemLabelGenerator, setItems, setItems, setItems, setItems, setItems, setItems, setItems, setItems, setItems, setItems, setItems, setItemsPageable, setItemsPageable, setItemsWithFilterConverter, setItemsWithFilterConverter, setManualValidation, setOpened, setPageSize, setRenderer, setRequired, validateMethods inherited from class com.vaadin.flow.component.AbstractSinglePropertyField
getSynchronizationRegistration, hasValidValue, setPresentationValue, setSynchronizedEventMethods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, getEmptyValue, isEmpty, setModelValue, valueEqualsMethods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.BlurNotifier
addBlurListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, focus, getTabIndex, setTabIndexMethods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListenerMethods inherited from interface com.vaadin.flow.component.shared.HasAllowedCharPattern
getAllowedCharPattern, setAllowedCharPatternMethods inherited from interface com.vaadin.flow.component.shared.HasAutoOpen
isAutoOpen, setAutoOpenMethods inherited from interface com.vaadin.flow.component.shared.HasClearButton
isClearButtonVisible, setClearButtonVisibleMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasHelper
getHelperComponent, getHelperText, setHelperComponent, setHelperTextMethods inherited from interface com.vaadin.flow.data.provider.HasListDataView
setItemsMethods inherited from interface com.vaadin.flow.component.HasPlaceholder
getPlaceholder, setPlaceholderMethods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeNameMethods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant
addThemeVariants, removeThemeVariants, setThemeVariant, setThemeVariants, setThemeVariantsMethods inherited from interface com.vaadin.flow.component.shared.HasTooltip
getTooltip, setTooltipMarkdown, setTooltipTextMethods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setErrorMessage, setInvalidMethods inherited from interface com.vaadin.flow.data.binder.HasValidator
addValidationStatusChangeListenerMethods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getOptionalValue, isEmptyMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, setReadOnlyMethods inherited from interface com.vaadin.flow.data.selection.MultiSelect
deselect, deselect, deselectAll, getEmptyValue, select, select
-
Constructor Details
-
MultiSelectComboBox
public MultiSelectComboBox()Default constructor. Creates an empty combo box. -
MultiSelectComboBox
public MultiSelectComboBox(int pageSize) Creates an empty combo box with the defined page size for lazy loading.The default page size is 50.
The page size is also the largest number of items that can support client-side filtering. If you provide more items than the page size, the component has to fall back to server-side filtering.
- Parameters:
pageSize- the amount of items to request at a time for lazy loading- See Also:
-
MultiSelectComboBox
Creates an empty combo box with the defined label.- Parameters:
label- the label describing the combo box- See Also:
-
MultiSelectComboBox
Creates a combo box with the defined label and populated with the items in the collection.- Parameters:
label- the label describing the combo boxitems- the items to be shown in the list of the combo box- See Also:
-
MultiSelectComboBox
Creates a combo box with the defined label and populated with the items in the array.- Parameters:
label- the label describing the combo boxitems- the items to be shown in the list of the combo box- See Also:
-
MultiSelectComboBox
public MultiSelectComboBox(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener) Constructs a combo box with a value change listener.- Parameters:
listener- the value change listener to add- See Also:
-
MultiSelectComboBox
public MultiSelectComboBox(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener) Constructs a combo box with the defined label and a value change listener.- Parameters:
label- the label describing the combo boxlistener- the value change listener to add- See Also:
-
MultiSelectComboBox
@SafeVarargs public MultiSelectComboBox(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener, TItem... items) Constructs a combo box with the defined label, a value change listener and populated with the items in the array.- Parameters:
label- the label describing the combo boxlistener- the value change listener to additems- the items to be shown in the list of the combo box- See Also:
-
-
Method Details
-
onAttach
Description copied from class:ComponentCalled when the component is attached to a UI.This method is invoked before the
AttachEventis fired for the component. Make sure to callsuper.onAttachwhen overriding this method.- Overrides:
onAttachin classComboBoxBase<MultiSelectComboBox<TItem>,TItem, Set<TItem>> - Parameters:
attachEvent- the attach event
-
setRequiredIndicatorVisible
public void setRequiredIndicatorVisible(boolean required) Sets whether the user is required to provide a value. When required, an indicator appears next to the label and the field invalidates if the value is cleared.NOTE: The required indicator is only visible when the field has a label, see
HasLabel.setLabel(String).- Specified by:
setRequiredIndicatorVisiblein interfaceHasValue<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,Set<TItem>>, Set<TItem>> - Specified by:
setRequiredIndicatorVisiblein interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,Set<TItem>>, Set<TItem>> - Overrides:
setRequiredIndicatorVisiblein classComboBoxBase<MultiSelectComboBox<TItem>,TItem, Set<TItem>> - Parameters:
required-trueto make the field required,falseotherwise- See Also:
-
isRequiredIndicatorVisible
public boolean isRequiredIndicatorVisible()Gets whether the user is required to provide a value.- Specified by:
isRequiredIndicatorVisiblein interfaceHasValue<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,Set<TItem>>, Set<TItem>> - Specified by:
isRequiredIndicatorVisiblein interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,Set<TItem>>, Set<TItem>> - Overrides:
isRequiredIndicatorVisiblein classComboBoxBase<MultiSelectComboBox<TItem>,TItem, Set<TItem>> - Returns:
trueif the field is required,falseotherwise- See Also:
-
getValue
Gets the value of the component, which is a set of selected items.The returned set is immutable and can not be modified. Use
MultiSelect.select(Object[])orMultiSelect.deselect(Object[])to add or remove individual items.- Specified by:
getValuein interfaceHasValue<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,Set<TItem>>, Set<TItem>> - Specified by:
getValuein interfaceMultiSelect<MultiSelectComboBox<TItem>,TItem> - Overrides:
getValuein classAbstractField<MultiSelectComboBox<TItem>,Set<TItem>> - Returns:
- an unmodifiable set of selected items
-
setValue
Sets the value of the component, which is a set of selected items.Note that it is allowed to pass
nullas value to clear the selection, but that an empty set will be stored as value instead.- Specified by:
setValuein interfaceHasValue<AbstractField.ComponentValueChangeEvent<MultiSelectComboBox<TItem>,Set<TItem>>, Set<TItem>> - Specified by:
setValuein interfaceMultiSelect<MultiSelectComboBox<TItem>,TItem> - Overrides:
setValuein classComboBoxBase<MultiSelectComboBox<TItem>,TItem, Set<TItem>> - Parameters:
value- the new value
-
setValue
Sets the value of the component, which is a set of selected items. As each item can only be selected once, duplicates in the provided items will be removed. Passing no items will result in an empty selection.- Parameters:
items- the new value
-
setValue
Sets the value of the component, which is a set of selected items. As each item can only be selected once, duplicates in the provided items will be removed. Passing no items will result in an empty selection.- Parameters:
items- the new value
-
refreshValue
protected void refreshValue()Description copied from class:ComboBoxBaseRefresh value / selection of the web component after changes that might affect the presentation / rendering of items- Specified by:
refreshValuein classComboBoxBase<MultiSelectComboBox<TItem>,TItem, Set<TItem>>
-
isSelected
Description copied from class:ComboBoxBaseWhether the item is currently selected in the combo box.- Specified by:
isSelectedin interfaceMultiSelect<MultiSelectComboBox<TItem>,TItem> - Specified by:
isSelectedin classComboBoxBase<MultiSelectComboBox<TItem>,TItem, Set<TItem>> - Parameters:
item- the item to check- Returns:
trueif the item is selected,falseotherwise
-
getSelectedItems
Description copied from interface:MultiSelectReturns an immutable set of the currently selected items. It is safe to invoke otherSelectionModelmethods while iterating over the set.Implementation note: the iteration order of the items in the returned set should be well-defined and documented by the implementing class.
- Specified by:
getSelectedItemsin interfaceMultiSelect<MultiSelectComboBox<TItem>,TItem> - Returns:
- the items in the current selection, not
null
-
addSelectionListener
public Registration addSelectionListener(MultiSelectionListener<MultiSelectComboBox<TItem>, TItem> listener) Description copied from interface:MultiSelectAdds a selection listener that will be called when the selection is changed either by the user or programmatically.- Specified by:
addSelectionListenerin interfaceMultiSelect<MultiSelectComboBox<TItem>,TItem> - Parameters:
listener- the value change listener, notnull- Returns:
- a registration for the listener
-
updateSelection
Description copied from interface:MultiSelectUpdates the selection by adding and removing the given items from it.If all the added items were already selected and the removed items were not selected, this is a NO-OP.
Duplicate items (in both add and remove sets) are ignored.
- Specified by:
updateSelectionin interfaceMultiSelect<MultiSelectComboBox<TItem>,TItem> - Parameters:
addedItems- the items to add, notnullremovedItems- the items to remove, notnull
-
getAutoExpand
Gets the behavior of the component when not all selected items can be displayed as chips within the current field width.- Returns:
- The current
MultiSelectComboBox.AutoExpandMode - Since:
- 24.3
-
setAutoExpand
Sets the behavior of the component when not all selected items can be displayed as chips within the current field width. Expansion only works with undefined size in the desired direction (i.e. setting `max-width` limits the component's width).- Since:
- 24.3
-
isSelectedItemsOnTop
public boolean isSelectedItemsOnTop()Gets whether selected items are grouped at the top of the overlay.- Returns:
trueif enabled,falseotherwise- Since:
- 24.3
-
setSelectedItemsOnTop
public void setSelectedItemsOnTop(boolean selectedItemsOnTop) Enables or disables grouping of the selected items at the top of the overlay.- Parameters:
selectedItemsOnTop-trueto group selected items at the top- Since:
- 24.3
-
isKeepFilter
public boolean isKeepFilter()Gets whether the filter is kept after selecting items.falseby default.- Returns:
trueif enabled,falseotherwise- Since:
- 24.4
-
setKeepFilter
public void setKeepFilter(boolean keepFilter) Enables or disables keeping the filter after selecting items. By default, the filter is cleared after selecting an item and the overlay shows the unfiltered list of items again. Enabling this option will keep the filter, which allows to select multiple filtered items in succession.- Parameters:
keepFilter- whether to keep the filter after selecting an item
-
getI18n
Gets the internationalization object previously set for this component.NOTE: Updating the instance that is returned from this method will not update the component if not set again using
setI18n(MultiSelectComboBoxI18n)- Overrides:
getI18nin classComboBoxBase<MultiSelectComboBox<TItem>,TItem, Set<TItem>> - Returns:
- the i18n object or
nullif no i18n object has been set
-
setI18n
Sets the internationalization object for this component.- Parameters:
i18n- the i18n object, notnull
-
setOverlayWidth
Sets the dropdown overlay width.- Parameters:
width- the new dropdown width. Pass in null to set the dropdown width back to the default value.
-
setOverlayWidth
Sets the dropdown overlay width. Negative number implies unspecified size (the dropdown width is reverted back to the default value).- Parameters:
width- the width of the dropdown.unit- the unit used for the dropdown.
-