Skip to content

Picker - Vant 4

Picker

Intro

The picker component is usually used with Popup Component.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

js
import { createApp } from'vue'; import { Picker } from'vant'; const app = createApp(); app.use(Picker);

Usage

Basic Usage

html
js
import { showToast } from'vant'; exportdefault { setup() { const columns = [ { text: 'Delaware', value: 'Delaware' }, { text: 'Florida', value: 'Florida' }, { text: 'Wenzhou', value: 'Wenzhou' }, { text: 'Indiana', value: 'Indiana' }, { text: 'Maine', value: 'Maine' }, ]; constonConfirm = ({ selectedValues }) => { showToast(`Value: ${selectedValues.join(',')}`); }; constonChange = ({ selectedValues }) => { showToast(`Value: ${selectedValues.join(',')}`); }; constonCancel = () => showToast('Cancel'); return { columns, onChange, onCancel, onConfirm, }; }, };

With Popup

html
js
import { ref } from'vue'; exportdefault { setup() { const columns = [ { text: 'Delaware', value: 'Delaware' }, { text: 'Florida', value: 'Florida' }, { text: 'Wenzhou', value: 'Wenzhou' }, { text: 'Indiana', value: 'Indiana' }, { text: 'Maine', value: 'Maine' }, ]; const fieldValue = ref(''); const pickerValue = ref<Numeric[]>([]); const showPicker = ref(false); constonConfirm = ({ selectedValues, selectedOptions }) => { showPicker.value = false; pickerValue.value = selectedValues; fieldValue.value = selectedOptions[0].text; }; return { columns, onConfirm, fieldValue, showPicker, }; }, };

v-model

Using v-model to bind selected values.

html
js
import { showToast } from'vant'; exportdefault { setup() { const columns = [ { text: 'Delaware', value: 'Delaware' }, { text: 'Florida', value: 'Florida' }, { text: 'Wenzhou', value: 'Wenzhou' }, { text: 'Indiana', value: 'Indiana' }, { text: 'Maine', value: 'Maine' }, ]; const selectedValues = ref(['Wenzhou']); return { columns, selectedValues, }; }, };

Multiple Columns

html
js
exportdefault { setup() { const columns = [ [ { text: 'Monday', value: 'Monday' }, { text: 'Tuesday', value: 'Tuesday' }, { text: 'Wednesday', value: 'Wednesday' }, { text: 'Thursday', value: 'Thursday' }, { text: 'Friday', value: 'Friday' }, ], [ { text: 'Morning', value: 'Morning' }, { text: 'Afternoon', value: 'Afternoon' }, { text: 'Evening', value: 'Evening' }, ], ]; return { columns }; }, };

Cascade

html
js
exportdefault { setup() { const columns = [ { text: 'Zhejiang', value: 'Zhejiang', children: [ { text: 'Hangzhou', value: 'Hangzhou', children: [ { text: 'Xihu', value: 'Xihu' }, { text: 'Yuhang', value: 'Yuhang' }, ], }, { text: 'Wenzhou', value: 'Wenzhou', children: [ { text: 'Lucheng', value: 'Lucheng' }, { text: 'Ouhai', value: 'Ouhai' }, ], }, ], }, { text: 'Fujian', value: 'Fujian', children: [ { text: 'Fuzhou', value: 'Fuzhou', children: [ { text: 'Gulou', value: 'Gulou' }, { text: 'Taijiang', value: 'Taijiang' }, ], }, { text: 'Xiamen', value: 'Xiamen', children: [ { text: 'Siming', value: 'Siming' }, { text: 'Haicang', value: 'Haicang' }, ], }, ], }, ]; return { columns }; }, };

Disable option

html
js
exportdefault { setup() { const columns = [ { text: 'Delaware', value: 'Delaware', disabled: true }, { text: 'Florida', value: 'Florida' }, { text: 'Wenzhou', value: 'Wenzhou' }, ]; return { columns }; }, };

Loading

When Picker columns data is acquired asynchronously, use loading prop to show loading prompt.

html
js
import { ref } from'vue'; exportdefault { setup() { const columns = ref([]); const loading = ref(true); setTimeout(() => { columns.value = [{ text: 'Option', value: 'option' }]; loading.value = false; }, 1000); return { columns, loading }; }, };

Empty content

When the data is empty, you can use the empty slot to customize the empty content.

html

Custom Columns Field

html
js
exportdefault { setup() { const columns = [ { cityName: 'Zhejiang', cities: [ { cityName: 'Hangzhou', cities: [{ cityName: 'Xihu' }, { cityName: 'Yuhang' }], }, { cityName: 'Wenzhou', cities: [{ cityName: 'Lucheng' }, { cityName: 'Ouhai' }], }, ], }, { cityName: 'Fujian', cities: [ { cityName: 'Fuzhou', cities: [{ cityName: 'Gulou' }, { cityName: 'Taijiang' }], }, { cityName: 'Xiamen', cities: [{ cityName: 'Siming' }, { cityName: 'Haicang' }], }, ], }, ]; const customFieldName = { text: 'cityName', value: 'cityName', children: 'cities', }; return { columns, customFieldName, }; }, };

API

Props

AttributeDescriptionTypeDefault
v-modelvalues of chosen option*number[]string[]*
columnsColumns data*PickerOption[]PickerOption[][]*
columns-field-namescustom columns fieldobject{ text: 'text', value: 'value', children: 'children' }
titleToolbar titlestring-
confirm-button-textText of confirm button, setting it as an empty string can hide the buttonstringConfirm
cancel-button-textText of cancel button, setting it as an empty string can hide the buttonstringCancel
toolbar-positionToolbar position, cat be set to bottomstringtop
loadingWhether to show loading promptbooleanfalse
readonlyWhether to be readonlybooleanfalse
show-toolbarWhether to show toolbarbooleantrue
allow-htmlWhether to allow HTML in option textbooleanfalse
option-heightOption height, supports px``vw``vh``rem unit, default px*numberstring*
visible-option-numCount of visible columns*numberstring*
swipe-durationDuration of the momentum animation, unit ms*numberstring*

Events

EventDescriptionArguments
confirmEmitted when the confirm button is clicked{ selectedValues, selectedOptions, selectedIndexes }
cancelEmitted when the cancel button is clicked{ selectedValues, selectedOptions, selectedIndexes }
changeEmitted when current selected option is changed{ selectedValues, selectedOptions,selectedIndexes, columnIndex }
click-optionEmitted when an option is clicked{ currentOption, selectedValues, selectedOptions, selectedIndexes, columnIndex }
scroll-into v4.2.1Emitted when an option is scrolled into the middle selection area by clicking or dragging{ currentOption, columnIndex }

Slots

NameDescriptionSlotProps
toolbarCustom toolbar content-
titleCustom title-
confirmCustom confirm button text-
cancelCustom cancel button text-
optionCustom option contentoption: PickerOption, index: number
columns-topCustom content above columns-
columns-bottomCustom content below columns-
empty v4.9.10Custom empty content-

Data Structure of PickerOption

KeyDescriptionType
textText*string
valueValue of option*string
disabledWhether to disable optionboolean
childrenCascade children optionsPickerOption[]
classNameClassName for this option*string

Methods

Use ref to get Picker instance and call instance methods.

NameDescriptionAttributeReturn value
confirmStop scrolling and emit confirm event--
getSelectedOptionsGet current selected options-*(PickerOption

Types

The component exports the following type definitions:

ts
importtype { PickerProps, PickerColumn, PickerOption, PickerInstance, PickerFieldNames, PickerToolbarPosition, PickerCancelEventParams, PickerChangeEventParams, PickerConfirmEventParams, } from'vant';

PickerInstance is the type of component instance:

ts
import { ref } from'vue'; importtype { PickerInstance } from'vant'; const pickerRef = ref<PickerInstance>(); pickerRef.value?.confirm();

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-picker-backgroundvar(--van-background-2)-
--van-picker-toolbar-height44px-
--van-picker-title-font-sizevar(--van-font-size-lg)-
--van-picker-title-line-heightvar(--van-line-height-md)-
--van-picker-action-padding0 var(--van-padding-md)-
--van-picker-action-font-sizevar(--van-font-size-md)-
--van-picker-confirm-action-colorvar(--van-primary-color)-
--van-picker-cancel-action-colorvar(--van-text-color-2)-
--van-picker-option-padding0 var(--van-padding-base)-
--van-picker-option-font-sizevar(--van-font-size-lg)-
--van-picker-option-text-colorvar(--van-text-color)-
--van-picker-option-disabled-opacity0.3-
--van-picker-mask-colorlinear-gradient-
--van-picker-loading-icon-colorvar(--van-primary-color)-
--van-picker-loading-mask-colorrgba(255, 255, 255, 0.9)-

Enterprise-level mobile solution based on Vant