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 as style and class 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. |
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. |
placeholder | string | null | Label to display when there are no selections. |
disabled | boolean | false | When present, it specifies that the component should be disabled. |
tabindex | string | null | Index of the element in tabbing order. |
inputId | string | null | Identifier of the underlying input element. |
dataKey | string | null | A property to uniquely identify an option. |
ariaLabelledBy | string | null | Establishes relationships between the component and label(s) where its value should be one or more element IDs. |
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. |
emptyFilterMessage | string | No results found | Text to display when filtering does not return any results. Defaults to value from PrimeVue locale configuration. |
emptyMessage | string | No results found | Text to display when there are no options available. Defaults to value from PrimeVue locale configuration. |
display | string | comma | Defines how the selected items are displayed, valid values are "comma" and "chip". |
panelClass | string | null | Style class of the overlay panel. |
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. |
loadingIcon | string | pi pi-spinner pi-spin | Icon to display in loading state. |
maxSelectedLabels | number | null | Decides how many selected item labels to show at most. |
selectedItemsLabel | string | {0} items selected | Label to display after exceeding max selected labels. |
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 |
Name | Parameters | Description |
---|---|---|
change | event.originalEvent: Original event event.value: Selected option value |
Callback to invoke on value change. |
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 | - | Shows the overlay. |
hide | - | Hides the overlay. |
clearFilter | - | Clears filter input. |
Name | Parameters |
---|---|
option | option: Option instance index: Index of the option |
optiongroup | option: OptionGroup instance index: Index of the option group |
value | value: Value of the component placeholder: Placeholder prop value |
header | value: Value of the component options: Displayed options |
footer | value: Value of the component options: Displayed options |
emptyfilter | - |
empty | - |
chip | value: A value in the selection |
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 |
indicator | - |
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. |
None.