import Listbox from 'primevue/listbox';
<script src="https://unpkg.com/primevue@^3/core/core.min.js"></script>
<script src="https://unpkg.com/primevue@^3/listbox/listbox.min.js"></script>
Listbox requires a value to bind and a collection of arbitrary objects along with the optionLabel property to specify the label property of the option.
<Listbox v-model="selectedCity" :options="cities" optionLabel="name" />
data() {
return {
selectedCity: null,
cities: [
{name: 'New York', code: 'NY'},
{name: 'Rome', code: 'RM'},
{name: 'London', code: 'LDN'},
{name: 'Istanbul', code: 'IST'},
{name: 'Paris', code: 'PRS'}
]
}
}
Listbox allows selection of either single or multiple items. In single case, model should be a single object reference whereas in multiple case should be an array. Multiple items can either be selected using metaKey or toggled individually depending on the value of metaKeySelection property value which is true by default. On touch enabled devices metaKeySelection is turned off automatically.
<Listbox v-model="selectedCity" :options="cities" optionLabel="name" :multiple="true"/>
Options groups are specified with the optionGroupLabel and optionGroupChildren properties.
export default {
data() {
return {
selectedGroupedCity: 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'}
]
}]
}
}
}
<Listbox v-model="selectedGroupedCity" :options="groupedCities"
optionLabel="label" optionGroupLabel="label" optionGroupChildren="items">
</Listbox>
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".
<Listbox v-model="selectedCity" :options="cities" optionLabel="name" :filter="true"/>
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 optiongroup, header, footer, emptyfilter and empty slots are provided for further customization.
<Listbox v-model="selectedCars" :options="cars" :multiple="true" :filter="true" optionLabel="brand" listStyle="max-height:250px" style="width:15em">
<template #option="slotProps">
<div>
<img :alt="slotProps.option.brand" :src="'demo/images/car/' + slotProps.option.brand + '.png'" />
<span>{{slotProps.option.brand}}</span>
</div>
</template>
</Listbox>
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. |
listStyle | string | null | Inline style of inner list element. |
disabled | boolean | false | When specified, disables the component. |
dataKey | string | null | A property to uniquely identify an option. |
multiple | boolean | false | When specified, allows selecting multiple values. |
metaKeySelection | boolean | true | Defines how multiple items can be selected, when true metaKey needs to be pressed to select or unselect an item and when set to false selection of each item can be toggled individually. On touch enabled devices, metaKeySelection is turned off automatically. |
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. |
filterInputProps | object | null | Uses to pass all properties of the HTMLInputElement to the filter input inside the component. |
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. |
selectOnFocus | boolean | false | When enabled, the focused option is selected. |
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 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. |
tabindex | number | 0 | Index of the element in tabbing order. |
filterIcon | string | pi pi-search | Icon to display in filter input |
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. |
filter |
event.originalEvent: Original event event.value: Filter value |
Callback to invoke on filter input. |
Name | Parameters |
---|---|
option |
option: Option instance index: Index of the option |
optiongroup |
option: OptionGroup instance index: Index of the option group |
header |
value: Value of the component options: Displayed options |
footer |
value: Value of the component options: Displayed options |
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-listbox | Main container element. |
p-listbox-header | Header element. |
p-listbox-list-wrapper | Container of list element. |
p-listbox-list | List element. |
p-listbox-item | An item in the list element. |
Value to describe the component can be provided aria-labelledby or aria-label props. The list element has a listbox role with the aria-multiselectable attribute that sets to true when multiple selection is enabled. Each list item has an option role with aria-selected and aria-disabled as their attributes.
If filtering is enabled, filterInputProps can be defined to give aria-* props to the input element. Alternatively filterPlaceholder is usually utilized by the screen readers as well.
<span id="lb">Options</span>
<ListBox aria-labelledby="lb" />
<ListBox aria-label="City" />
Key | Function |
---|---|
tab | Moves focus to the first selected option, if there is none then first option receives the focus. |
up arrow | Moves focus to the previous option. |
down arrow | Moves focus to the next option. |
enter | Toggles the selected state of the focused option. |
space | Toggles the selected state of the focused option. |
home | Moves focus to the first option. |
end | Moves focus to the last option. |
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. |
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 |
---|---|
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 component. If there is none, moves focus to next element in page. |
None.