import MultiSelect from 'primevue/multiselect';
<script src="https://unpkg.com/primevue@^3/core/core.min.js"></script>
<script src="https://unpkg.com/primevue@^3/multiselect/multiselect.min.js"></script>
MultiSelect requires a value to bind and a collection of arbitrary objects along with the optionLabel property to specify the label property of the option.
<MultiSelect v-model="selectedCars" :options="cars" optionLabel="brand" placeholder="Select Brands" />
data() {
return {
selectedCars: null,
cars: [
{brand: 'Audi', value: 'Audi'},
{brand: 'BMW', value: 'BMW'},
{brand: 'Fiat', value: 'Fiat'},
{brand: 'Honda', value: 'Honda'},
{brand: 'Jaguar', value: 'Jaguar'},
{brand: 'Mercedes', value: 'Mercedes'},
{brand: 'Renault', value: 'Renault'},
{brand: 'Volkswagen', value: 'Volkswagen'},
{brand: 'Volvo', value: 'Volvo'}
]
}
}
A comma separated list is used by default to display selected items whereas alternative chip mode is provided using the display property to visualize the items as tokens.
<MultiSelect v-model="selectedCars" :options="cars" optionLabel="brand" placeholder="Select Brands" display="chip"/>
Options groups are specified with the optionGroupLabel and optionGroupChildren properties.
export default {
data() {
return {
selectedGroupedCities: null,
groupedCities: [{
label: 'Germany', code: 'DE',
items: [
{label: 'Berlin', value: 'Berlin'},
{label: 'Frankfurt', value: 'Frankfurt'},
{label: 'Hamburg', value: 'Hamburg'},
{label: 'Munich', value: 'Munich'}
]
},
{
label: 'USA', code: 'US',
items: [
{label: 'Chicago', value: 'Chicago'},
{label: 'Los Angeles', value: 'Los Angeles'},
{label: 'New York', value: 'New York'},
{label: 'San Francisco', value: 'San Francisco'}
]
},
{
label: 'Japan', code: 'JP',
items: [
{label: 'Kyoto', value: 'Kyoto'},
{label: 'Osaka', value: 'Osaka'},
{label: 'Tokyo', value: 'Tokyo'},
{label: 'Yokohama', value: 'Yokohama'}
]
}]
}
}
}
<MultiSelect v-model="selectedGroupedCities" :options="groupedCities"
optionLabel="label" optionGroupLabel="label" optionGroupChildren="items">
</MultiSelect>
Filtering allows searching items in the list using an input field at the header. In order to use filtering, enable filter property. By default, optionLabel is used when searching and filterFields can be used to customize the fields being utilized. Furthermore, filterMatchMode is available to define the search algorithm. Valid values are "contains" (default), "startsWith" and "endsWith".
<MultiSelect v-model="selectedCars" :options="cars" :filter="true" optionLabel="brand" placeholder="Select Brands"/>
Label of an option is used as the display text of an item by default, for custom content support define an option template that gets the option instance as a parameter. In addition value, optiongroup, chip, header, footer, emptyfilter and empty slots are provided for further customization.
<MultiSelect v-model="selectedCars2" :options="cars" optionLabel="brand" placeholder="Select a Car">
<template #value="slotProps">
<div class="p-multiselect-car-token" v-for="option of slotProps.value" :key="option.brand">
<img :alt="option.brand" :src="'demo/images/car/' + option.brand + '.png'" />
<span>{{option.brand}}</span>
</div>
<template v-if="!slotProps.value || slotProps.value.length === 0">
Select Brands
</template>
</template>
<template #option="slotProps">
<div class="p-multiselect-car-option">
<img :alt="slotProps.option.brand" :src="'demo/images/car/' + slotProps.option.brand + '.png'" />
<span>{{slotProps.option.brand}}</span>
</div>
</template>
</MultiSelect>
Any property of HTMLDivElement are passed to the main container element. Following are the additional properties to configure the component.
Name | Type | Default | Description |
---|---|---|---|
modelValue | any | null | Value of the component. |
options | array | null | An array of selectitems to display as the available options. |
optionLabel | string | function | null | Property name or getter function to use as the label of an option. |
optionValue | string | function | null | Property name or getter function to use as the value of an option, defaults to the option itself when not defined. |
optionDisabled | string | function | null | Property name or getter function to use as the disabled flag of an option, defaults to false when not defined. |
optionGroupLabel | string | function | null | Property name or getter function to use as the label of an option group. |
optionGroupChildren | string | function | null | Property name or getter function that refers to the children options of option group. |
scrollHeight | string | 200px | Height of the viewport, a scrollbar is defined if height of list exceeds this value. |
placeholder | string | null | Label to display when there are no selections. |
disabled | boolean | false | When present, it specifies that the component should be disabled. |
inputId | string | null | Identifier of the underlying input element. |
inputProps | object | null | Uses to pass all properties of the HTMLInputElement to the focusable input element inside the component. |
panelStyle | any | null | Inline style of the overlay panel. |
panelClass | string | null | Style class of the overlay panel. |
panelProps | object | null | Uses to pass all properties of the HTMLDivElement to the overlay panel. |
filterInputProps | object | null | Uses to pass all properties of the HTMLInputElement to the filter input inside the overlay panel. |
closeButtonProps | object | null | Uses to pass all properties of the HTMLButtonElement to the close button inside the overlay panel. |
dataKey | string | null | A property to uniquely identify an option. |
filter | boolean | false | When specified, displays a filter input at header. |
filterPlaceholder | string | null | Placeholder text to show when filter input is empty. |
filterLocale | string | undefined | Locale to use in filtering. The default locale is the host environment's current locale. |
filterMatchMode | string | contains | Defines the filtering algorithm to use when searching the options. Valid values are "contains" (default), "startsWith" and "endsWith" |
filterFields | array | null | Fields used when filtering the options, defaults to optionLabel. |
appendTo | string | body | A valid query selector or an HTMLElement to specify where the overlay gets attached. Special keywords are "body" for document body and "self" for the element itself. |
display | string | comma | Defines how the selected items are displayed, valid values are "comma" and "chip". |
selectedItemsLabel | string | {0} items selected | Label to display after exceeding max selected labels. |
maxSelectedLabels | number | null | Decides how many selected item labels to show at most. |
selectionLimit | number | null | Maximum number of selectable items. |
showToggleAll | boolean | true | Whether to show the header checkbox to toggle the selection of all items at once. |
loading | boolean | false | Whether the multiselect is in loading state. |
checkboxIcon | string | pi pi-check | Icon to display in the checkboxes. |
closeIcon | string | pi pi-times | Icon to display in the dropdown close button. |
dropdownIcon | string | pi pi-chevron-down | Icon to display in dropdown. |
filterIcon | string | pi pi-search | Icon to display in filter input. |
loadingIcon | string | pi pi-spinner pi-spin | Icon to display in loading state. |
removeTokenIcon | string | pi pi-times-circle | Icon to display in chip remove action. |
selectAll | boolean | false | Whether all data is selected. |
resetFilterOnHide | boolean | false | Clears the filter value when hiding the dropdown. |
virtualScrollerOptions | object | null | Whether to use the virtualScroller feature. The properties of |
autoOptionFocus | boolean | true | Whether to focus on the first visible or selected element when the overlay panel is shown. |
autoFilterFocus | boolean | false | Whether to focus on the filter element when the overlay panel is shown. |
filterMessage | string | {0} results are available | Text to be displayed in hidden accessible field when filtering returns any results. Defaults to value from PrimeVue locale configuration. |
selectionMessage | string | {0} items selected | Text to be displayed in hidden accessible field when options are selected. Defaults to value from PrimeVue locale configuration. |
emptySelectionMessage | string | No selected item | Text to be displayed in hidden accessible field when any option is not selected. Defaults to value from PrimeVue locale configuration. |
emptyFilterMessage | string | No results found | Text to be displayed when filtering does not return any results. Defaults to value from PrimeVue locale configuration. |
emptyMessage | string | No results found | Text to be displayed when there are no options available. Defaults to value from PrimeVue locale configuration. |
tabindex | number | 0 | Index of the element in tabbing order. |
aria-label | string | null | Defines a string value that labels an interactive element. |
aria-labelledby | string | null | Establishes relationships between the component and label(s) where its value should be one or more element IDs. |
Name | Parameters | Description |
---|---|---|
change |
event.originalEvent: Original event event.value: Selected option value |
Callback to invoke on value change. |
focus | event | Callback to invoke when the component receives focus. |
blur | event | Callback to invoke when the component loses focus. |
before-show | - | Callback to invoke before the overlay is shown. |
before-hide | - | Callback to invoke before the overlay is hidden. |
show | - | Callback to invoke when the overlay is shown. |
hide | - | Callback to invoke when the overlay is hidden. |
filter |
event.originalEvent: Original event event.value: Filter value |
Callback to invoke on filter input. |
selectall-change |
event.originalEvent: Original event event.checked: Whether all data is selected. |
Callback to invoke when all data is selected. |
Name | Parameters | Description |
---|---|---|
show | isFocus: Decides whether to focus on the component. Default value is false. | Shows the overlay. |
hide | isFocus: Decides whether to focus on the component. Default value is false. | Hides the overlay. |
clearFilter | - | Clears filter input. |
Name | Parameters |
---|---|
value |
value: Value of the component placeholder: Placeholder prop value |
chip | value: A value in the selection |
indicator | - |
header |
value: Value of the component options: Displayed options |
footer |
value: Value of the component options: Displayed options |
option |
option: Option instance index: Index of the option |
optiongroup |
option: OptionGroup instance index: Index of the option group |
emptyfilter | - |
empty | - |
content |
items: An array of objects to display for virtualscroller styleClass: Style class of the component contentRef: Referance of the content getItemOptions: Options of the items |
loader | options: Options of the loader items for virtualscroller |
Following is the list of structural style classes, for theming classes visit
Name | Element |
---|---|
p-multiselect | Container element. |
p-multiselect-label-container | Container of the label to display selected items. |
p-multiselect-label-container | Label to display selected items. |
p-multiselect-trigger | Dropdown button. |
p-multiselect-filter-container | Container of filter input. |
p-multiselect-panel | Overlay panel for items. |
p-multiselect-items | List container of items. |
p-multiselect-item | An item in the list. |
p-overlay-open | Container element when overlay is visible. |
Value to describe the component can either be provided with aria-labelledby or aria-label props. The multiselect component has a combobox role in addition to aria-haspopup and aria-expanded attributes. The relation between the combobox and the popup is created with aria-controls attribute that refers to the id of the popup listbox.
The popup listbox uses listbox as the role with aria-multiselectable enabled. Each list item has an option role along with aria-label, aria-selected and aria-disabled attributes.
Checkbox component at the header uses a hidden native checkbox element internally that is only visible to screen readers. Value to read is defined with the selectAll and unselectAll keys of the aria property from the
If filtering is enabled, filterInputProps can be defined to give aria-* props to the input element.
Close button uses close key of the aria property from the
<span id="dd1">Options</span>
<MultiSelect aria-labelledby="dd1" />
<MultiSelect aria-label="Options" />
Key | Function |
---|---|
tab | Moves focus to the multiselect element. |
space | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
enter | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
down arrow | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
up arrow | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
any printable character | Opens the popup and moves focus to the option whose label starts with the characters being typed, if there is none then first option receives the focus. |
Key | Function |
---|---|
tab | Moves focus to the next focusable element in the popup, if there is none then first focusable element receives the focus. |
shift + tab | Moves focus to the previous focusable element in the popup, if there is none then last focusable element receives the focus. |
enter | Toggles the selection state of the focused option, then moves focus to the multiselect element. |
space | Toggles the selection state of the focused option, then moves focus to the multiselect element. |
escape | Closes the popup, moves focus to the multiselect element. |
down arrow | Moves focus to the next option, if there is none then visual focus does not change. |
up arrow | Moves focus to the previous option, if there is none then visual focus does not change. |
alt + up arrow | Selects the focused option and closes the popup, then moves focus to the multiselect element. |
shift + down arrow | Moves focus to the next option and toggles the selection state. |
shift + up arrow | Moves focus to the previous option and toggles the selection state. |
shift + space | Selects the items between the most recently selected option and the focused option. |
home | Moves focus to the first option. |
end | Moves focus to the last option. |
control + shift + home | Selects the focused options and all the options up to the first one. |
control + shift + end | Selects the focused options and all the options down to the last one. |
control + a | Selects all options. |
pageUp | Jumps visual focus to first option. |
pageDown | Jumps visual focus to last option. |
any printable character | Moves focus to the option whose label starts with the characters being typed. |
Key | Function |
---|---|
space | Toggles the checked state. |
escape | Closes the popup and moves focus to the multiselect element. |
Key | Function |
---|---|
down arrow | Moves focus to the next option, if there is none then visual focus does not change. |
up arrow | Moves focus to the previous option, if there is none then visual focus does not change. |
left arrow | Removes the visual focus from the current option and moves input cursor to one character left. |
right arrow | Removes the visual focus from the current option and moves input cursor to one character right. |
home | Moves input cursor at the end, if not then moves focus to the first option. |
end | Moves input cursor at the beginning, if not then moves focus to the last option. |
enter | Closes the popup and moves focus to the multiselect element. |
escape | Closes the popup and moves focus to the multiselect element. |
tab | Moves focus to the next focusable element in the popup. If there is none, the focusable option is selected and the overlay is closed then moves focus to next element in page. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the multiselect element. |
space | Closes the popup and moves focus to the multiselect element. |
escape | Closes the popup and moves focus to the multiselect element. |
None.