Picker 选择器

介绍

提供多个选项集合供用户选择,支持单列选择和多列级联,通常与弹出层组件配合使用。

引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

  1. import { createApp } from 'vue';
  2. import { Picker } from 'vant';
  3. const app = createApp();
  4. app.use(Picker);

代码演示

基础用法

选项配置

Picker 组件通过 columns 属性配置选项数据,columns 是一个包含字符串或对象的数组。

顶部栏

顶部栏包含标题、确认按钮和取消按钮,点击确认按钮触发 confirm 事件,点击取消按钮触发 cancel 事件。

  1. <van-picker
  2. title="标题"
  3. :columns="columns"
  4. @confirm="onConfirm"
  5. @cancel="onCancel"
  6. @change="onChange"
  7. />
  1. import { Toast } from 'vant';
  2. export default {
  3. setup() {
  4. const columns = ['杭州', '宁波', '温州', '绍兴', '湖州', '嘉兴', '金华'];
  5. const onConfirm = (value, index) => {
  6. Toast(`当前值: ${value}, 当前索引: ${index}`);
  7. };
  8. const onChange = (value, index) => {
  9. Toast(`当前值: ${value}, 当前索引: ${index}`);
  10. };
  11. const onCancel = () => Toast('取消');
  12. return {
  13. columns,
  14. onChange,
  15. onCancel,
  16. onConfirm,
  17. };
  18. },
  19. };

默认选中项

单列选择时,可以通过 default-index 属性设置初始选中项的索引。

  1. <van-picker title="标题" :columns="columns" :default-index="2" />

多列选择

columns 属性可以通过对象数组的形式配置多列选择,对象中可以配置选项数据、初始选中项等,详细格式见下方表格

  1. <van-picker title="标题" :columns="columns" />
  1. export default {
  2. setup() {
  3. const columns = [
  4. // 第一列
  5. {
  6. values: ['周一', '周二', '周三', '周四', '周五'],
  7. defaultIndex: 2,
  8. },
  9. // 第二列
  10. {
  11. values: ['上午', '下午', '晚上'],
  12. defaultIndex: 1,
  13. },
  14. ];
  15. return { columns };
  16. },
  17. };

级联选择

使用 columnschildren 字段可以实现选项级联的效果。如果级联层级较多,推荐使用 Cascader 级联选项组件

  1. <van-picker title="标题" :columns="columns" />
  1. export default {
  2. setup() {
  3. const columns = [
  4. {
  5. text: '浙江',
  6. children: [
  7. {
  8. text: '杭州',
  9. children: [{ text: '西湖区' }, { text: '余杭区' }],
  10. },
  11. {
  12. text: '温州',
  13. children: [{ text: '鹿城区' }, { text: '瓯海区' }],
  14. },
  15. ],
  16. },
  17. {
  18. text: '福建',
  19. children: [
  20. {
  21. text: '福州',
  22. children: [{ text: '鼓楼区' }, { text: '台江区' }],
  23. },
  24. {
  25. text: '厦门',
  26. children: [{ text: '思明区' }, { text: '海沧区' }],
  27. },
  28. ],
  29. },
  30. ];
  31. return { columns };
  32. },
  33. };

级联选择的数据嵌套深度需要保持一致,如果部分选项没有子选项,可以使用空字符串进行占位。

禁用选项

选项可以为对象结构,通过设置 disabled 来禁用该选项。

  1. <van-picker :columns="columns" />
  1. export default {
  2. setup() {
  3. const columns = [
  4. { text: '杭州', disabled: true },
  5. { text: '宁波' },
  6. { text: '温州' },
  7. ];
  8. return { columns };
  9. },
  10. };

动态设置选项

通过 Picker 上的实例方法可以更灵活地控制选择器,比如使用 setColumnValues 方法实现多列联动。

  1. <van-picker ref="picker" :columns="columns" @change="onChange" />
  1. import { ref } from 'vue';
  2. export default {
  3. setup() {
  4. const picker = ref(null);
  5. const cities = {
  6. 浙江: ['杭州', '宁波', '温州', '嘉兴', '湖州'],
  7. 福建: ['福州', '厦门', '莆田', '三明', '泉州'],
  8. };
  9. const columns = [
  10. { values: Object.keys(cities) },
  11. { values: cities['浙江'] },
  12. ];
  13. const onChange = (values) => {
  14. picker.value.setColumnValues(1, cities[values[0]]);
  15. };
  16. return {
  17. picker,
  18. columns,
  19. onChange,
  20. };
  21. },
  22. };

加载状态

若选择器数据是异步获取的,可以通过 loading 属性显示加载提示。

  1. <van-picker :columns="columns" :loading="loading" />
  1. import { ref } from 'vue';
  2. export default {
  3. setup() {
  4. const columns = ref([]);
  5. const loading = ref(true);
  6. setTimeout(() => {
  7. columns.value = ['选项'];
  8. loading.value = false;
  9. }, 1000);
  10. return { columns, loading };
  11. },
  12. };

搭配弹出层使用

在实际场景中,Picker 通常作为用于辅助表单填写,可以搭配 Popup 和 Field 实现该效果。

  1. <van-field
  2. v-model="value"
  3. is-link
  4. readonly
  5. label="城市"
  6. placeholder="选择城市"
  7. @click="showPicker = true"
  8. />
  9. <van-popup v-model:show="showPicker" round position="bottom">
  10. <van-picker
  11. :columns="columns"
  12. @cancel="showPicker = false"
  13. @confirm="onConfirm"
  14. />
  15. </van-popup>
  1. import { ref } from 'vue';
  2. export default {
  3. setup() {
  4. const columns = ['杭州', '宁波', '温州', '绍兴', '湖州', '嘉兴', '金华'];
  5. const result = ref('');
  6. const showPicker = ref(false);
  7. const onConfirm = (value) => {
  8. result.value = value;
  9. showPicker.value = false;
  10. };
  11. return {
  12. result,
  13. columns,
  14. onConfirm,
  15. showPicker,
  16. };
  17. },
  18. };

自定义 Columns 的结构

  1. <van-picker
  2. :title="标题"
  3. :columns="columns"
  4. :columns-field-names="customFieldName"
  5. />
  1. export default {
  2. setup() {
  3. const columns = [
  4. {
  5. cityName: '浙江',
  6. cities: [
  7. {
  8. cityName: '杭州',
  9. cities: [{ cityName: '西湖区' }, { cityName: '余杭区' }],
  10. },
  11. {
  12. cityName: '温州',
  13. cities: [{ cityName: '鹿城区' }, { cityName: '瓯海区' }],
  14. },
  15. ],
  16. },
  17. {
  18. cityName: '福建',
  19. cities: [
  20. {
  21. cityName: '福州',
  22. cities: [{ cityName: '鼓楼区' }, { cityName: '台江区' }],
  23. },
  24. {
  25. cityName: '厦门',
  26. cities: [{ cityName: '思明区' }, { cityName: '海沧区' }],
  27. },
  28. ],
  29. },
  30. ];
  31. const customFieldName = {
  32. text: 'cityName',
  33. children: 'cities',
  34. };
  35. return {
  36. columns,
  37. customFieldName,
  38. };
  39. },
  40. };

API

Props

参数说明类型默认值
columns对象数组,配置每一列显示的数据Column[][]
columns-field-names自定义 columns 结构中的字段object{ text: ‘text’, values: ‘values’, children: ‘children’ }
title顶部栏标题string-
confirm-button-text确认按钮文字string确认
cancel-button-text取消按钮文字string取消
toolbar-position顶部栏位置,可选值为 bottomstringtop
loading是否显示加载状态booleanfalse
show-toolbar是否显示顶部栏booleantrue
allow-html是否允许选项内容中渲染 HTMLbooleanfalse
default-index单列选择时,默认选中项的索引number | string0
item-height选项高度,支持 px vw vh rem 单位,默认 pxnumber | string44
visible-item-count可见的选项个数number | string6
swipe-duration快速滑动时惯性滚动的时长,单位 msnumber | string1000

Events

当选择器有多列时,事件回调参数会返回数组。

事件名说明回调参数
confirm点击完成按钮时触发单列:选中值,选中值对应的索引
多列:所有列选中值,所有列选中值对应的索引
cancel点击取消按钮时触发单列:选中值,选中值对应的索引
多列:所有列选中值,所有列选中值对应的索引
change选项改变时触发单列:选中值,选中值对应的索引
多列:所有列选中值,当前列对应的索引

Slots

名称说明参数
toolbar v3.1.2自定义整个顶部栏的内容-
title自定义标题内容-
confirm自定义确认按钮内容-
cancel自定义取消按钮内容-
option自定义选项内容option: string | object
columns-top自定义选项上方内容-
columns-bottom自定义选项下方内容-

Column 数据结构

当传入多列数据时,columns 为一个对象数组,数组中的每一个对象配置每一列,每一列有以下 key:

键名说明类型
values列中对应的备选值Array<string | number>
defaultIndex初始选中项的索引,默认为 0number
className为对应列添加额外的类名string | Array | object
children级联选项Column

方法

通过 ref 可以获取到 Picker 实例并调用实例方法,详见组件实例方法

方法名说明参数返回值
getValues获取所有列选中的值-values
setValues设置所有列选中的值values-
getIndexes获取所有列选中值对应的索引-indexes
setIndexes设置所有列选中值对应的索引indexes-
getColumnValue获取对应列选中的值columnIndexvalue
setColumnValue设置对应列选中的值columnIndex, value-
getColumnIndex获取对应列选中项的索引columnIndexoptionIndex
setColumnIndex设置对应列选中项的索引columnIndex, optionIndex-
getColumnValues获取对应列中所有选项columnIndexvalues
setColumnValues设置对应列中所有选项columnIndex, values-
confirm停止惯性滚动并触发 confirm 事件--

类型定义

组件导出以下类型定义:

  1. import type {
  2. PickerProps,
  3. PickerColumn,
  4. PickerOption,
  5. PickerInstance,
  6. PickerFieldNames,
  7. PickerObjectColumn,
  8. PickerObjectOption,
  9. PickerToolbarPosition,
  10. } from 'vant';

PickerInstance 是组件实例的类型,用法如下:

  1. import { ref } from 'vue';
  2. import type { PickerInstance } from 'vant';
  3. const pickerRef = ref<PickerInstance>();
  4. pickerRef.value?.confirm();

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值描述
—van-picker-background-colorvar(—van-background-color-light)-
—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-text-link-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)-

常见问题

在桌面端无法操作组件?

参见桌面端适配

Picker 选择器 - 图1