Picker 选择器
17:06说明
底部弹出的选择器,用布尔 v-model 控制弹出与收起,columns 数据全部由外部传入。支持 selector(单列)、multiSelector(多列独立)、cascade(树形 children 级联)三种模式,confirm / change 回传含 selectedIndex、selectedValues、selectedOptions 的事件对象。
兼容性
| 平台 | 支持情况 |
|---|---|
| App | ✅ 支持 |
| H5 | ✅ 支持 |
| 微信小程序 | ✅ 支持 |
示例
单列选择
mode=selector,columns 传一列数据。
查看源码
vue
<template>
<ux-button size="small" shape="round" @click="show = true">打开选择器</ux-button>
<text v-if="result !== ''" class="result">选择结果: {{ result }}</text>
<ux-picker v-model="show" mode="selector" :columns="columns" :default-index="[2]" title="单选选择器" @confirm="onConfirm"></ux-picker>
</template>
<script setup lang="uts">
import type { PickerOption } from '@/uni_modules/uview-pro-x';
const show = ref<boolean>(false);
const result = ref<string>('');
const columns: PickerOption[][] = [
[
{ text: '春眠不觉晓', value: 1 },
{ text: '处处闻啼鸟', value: 2 },
{ text: '夜来风雨声', value: 3 },
{ text: '花落知多少', value: 4 }
]
];
function onConfirm(event: UTSJSONObject): void {
const values = event['selectedValues'] as string[];
if (values == null) return;
result.value = values.join(' / ');
}
</script>
<style scoped>
.result {
margin-top: 10px;
font-size: 13px;
color: #909399;
}
</style>多列选择
mode=multiSelector,columns 传多列独立数据。
查看源码
vue
<template>
<ux-button size="small" shape="round" @click="show = true">打开选择器</ux-button>
<text v-if="result !== ''" class="result">选择结果: {{ result }}</text>
<ux-picker v-model="show" mode="multiSelector" :columns="columns" :default-index="[0, 0, 1]" title="多列选择器" @confirm="onConfirm"></ux-picker>
</template>
<script setup lang="uts">
import type { PickerOption } from '@/uni_modules/uview-pro-x';
const show = ref<boolean>(false);
const result = ref<string>('');
const columns: PickerOption[][] = [
[
{ text: '亚洲', value: 'asia' },
{ text: '欧洲', value: 'europe' }
],
[
{ text: '中国', value: 'cn' },
{ text: '日本', value: 'jp' }
],
[
{ text: '北京', value: 'bj' },
{ text: '上海', value: 'sh' },
{ text: '广州', value: 'gz' }
]
];
function onConfirm(event: UTSJSONObject): void {
const values = event['selectedValues'] as string[];
if (values == null) return;
result.value = values.join(' / ');
}
</script>
<style scoped>
.result {
margin-top: 10px;
font-size: 13px;
color: #909399;
}
</style>级联联动
mode=cascade,通过 children 树形数据实现省市区联动。
查看源码
vue
<template>
<ux-button size="small" shape="round" @click="show = true">打开选择器</ux-button>
<text v-if="result !== ''" class="result">选择结果: {{ result }}</text>
<ux-picker v-model="show" mode="cascade" :columns="columns" :default-index="[0, 0, 0]" title="级联选择器" @confirm="onConfirm"></ux-picker>
</template>
<script setup lang="uts">
import type { PickerOption } from '@/uni_modules/uview-pro-x';
const show = ref<boolean>(false);
const result = ref<string>('');
const columns: PickerOption[][] = [
[
{
text: '山东省',
children: [
{
text: '青岛市',
children: [
{ text: '崂山区' },
{ text: '市南区' }
]
},
{
text: '济南市',
children: [
{ text: '历下区' },
{ text: '市中区' }
]
}
]
}
]
];
function onConfirm(event: UTSJSONObject): void {
const values = event['selectedValues'] as string[];
if (values == null) return;
result.value = values.join(' / ');
}
</script>
<style scoped>
.result {
margin-top: 10px;
font-size: 13px;
color: #909399;
}
</style>自定义样式
自定义标题、取消/确认按钮颜色。
查看源码
vue
<template>
<ux-button size="small" shape="round" @click="show = true">打开选择器</ux-button>
<text v-if="result !== ''" class="result">选择结果: {{ result }}</text>
<ux-picker v-model="show" mode="selector" :columns="columns" title="自定义颜色" cancel-color="#909399" confirm-color="#fa3534" @confirm="onConfirm"></ux-picker>
</template>
<script setup lang="uts">
import type { PickerOption } from '@/uni_modules/uview-pro-x';
const show = ref<boolean>(false);
const result = ref<string>('');
const columns: PickerOption[][] = [
[
{ text: '红色', value: 'red' },
{ text: '蓝色', value: 'blue' },
{ text: '绿色', value: 'green' }
]
];
function onConfirm(event: UTSJSONObject): void {
const values = event['selectedValues'] as string[];
if (values == null) return;
result.value = values.join(' / ');
}
</script>
<style scoped>
.result {
margin-top: 10px;
font-size: 13px;
color: #909399;
}
</style>API
Props
| 属性 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
customStyle | UTSJSONObject | - | - | |
customClass | string | '' | - | |
modelValue | v-model 控制弹出与收起 | boolean | false | - |
columns | 列数据,全部由外部传入: selector 传 [[a, b, c]]; multiSelector 传 [[a, b], [c, d]]; cascade 传 [[根节点]],通过 children 展开联动 | PickerOption[][] | - | - |
mode | 模式,selector-单列,multiSelector-多列,cascade-级联联动 | PickerMode | 'selector' | - |
defaultIndex | 默认选中的下标 | number[] | - | - |
title | 顶部标题 | string | '' | - |
cancelText | 取消按钮文字 | string | '' | - |
confirmText | 确认按钮文字 | string | '' | - |
cancelColor | 取消按钮颜色 | string | '' | - |
confirmColor | 确认按钮颜色 | string | '' | - |
maskCloseAble | 是否允许点击遮罩关闭 | boolean | true | - |
safeAreaInsetBottom | 是否开启底部安全区适配(默认 true,由 ux-popup 在 mode=bottom 时加 padding-bottom) | boolean | true | - |
zIndex | 弹出 z-index | number | string | 0 | - |
Events
| 事件名 | 说明 | 回调参数 | 版本 |
|---|---|---|---|
confirm | event: UTSJSONObject | - | |
cancel | - | - | |
change | event: UTSJSONObject | - |
Slots
| 插槽名 | 说明 | 版本 |
|---|---|---|
title | 自定义顶部标题区内容(默认渲染 title 文本) | - |
注意事项
- 必须用布尔
v-model控制弹出与收起,columns数据全部由外部传入。 mode取值:selector([[a, b, c]]单列)、multiSelector([[a, b], [c, d]]多列独立)、cascade([[根节点]],通过children树形联动)。confirm/change回调参数为对象,含selectedIndex、selectedValues(文本数组)、selectedOptions(选项对象数组)。default-index为各列默认选中下标,长度需与列数匹配,否则按 0 起算。title插槽可完全自定义标题区(默认渲染title文本)。- 底部安全区由内部
ux-popup处理(safe-area-inset-bottom);z-index为 0 时回退到$u.zIndex.popup。