Skip to main content
Version: 1.x

PickerView

嵌入页面的滚动选择器

属性

属性名类型说明
valueNumberArray数组中的数字依次表示 picker-view 内的 picker-view-column 选择的第几项(下标从 0 开始),数字大于 picker-view-column 可选项长度时,选择最后一项。
indicatorStyleString设置选择器中间选中框的样式
indicatorClassString设置选择器中间选中框的类名
maskStyleString设置蒙层的样式
maskClassString设置蒙层的类名
onChangeEventHandle当滚动选择,value 改变时触发 change 事件,event.detail = {value: value};value 为数组,表示 picker-view 内的 picker-view-column 当前选择的是第几项(下标从 0 开始)

各端支持度

属性微信小程序H5ReactNative百度小程序支付宝小程序字节跳动小程序
valuex
indicatorStylex
indicatorClassx
maskStylex
maskClassx
onChangex
PickerViewColumn
仅可放置于 PickerView 中,其孩子节点的高度会自动设置成与 PickerView 的选中框的高度一致
示例:
import Taro, { Component } from '@tarojs/taro'
import { View, PickerView, PickerViewColumn } from '@tarojs/components'

export default class Picks extends Component {

constructor () {
super(...arguments)
const date = new Date()
const years = []
const months = []
const days = []
for (let i = 1990; i <= date.getFullYear(); i++) {
years.push(i)
}
for (let i = 1; i <= 12; i++) {
months.push(i)
}
for (let i = 1; i <= 31; i++) {
days.push(i)
}
this.state = {
years: years,
year: date.getFullYear(),
months: months,
month: 2,
days: days,
day: 2,
value: [9999, 1, 1]
}
}

onChange = e => {
const val = e.detail.value
this.setState({
year: this.state.years[val[0]],
month: this.state.months[val[1]],
day: this.state.days[val[2]],
value: val
})
}

render() {
return (
<View>
<View>{this.state.year}{this.state.month}{this.state.day}</View>
<PickerView indicatorStyle='height: 50px;' style='width: 100%; height: 300px;' value={this.state.value} onChange={this.onChange}>
<PickerViewColumn>
{this.state.years.map(item => {
return (
<View>{item}</View>
);
})}
</PickerViewColumn>
<PickerViewColumn>
{this.state.months.map(item => {
return (
<View>{item}</View>
)
})}
</PickerViewColumn>
<PickerViewColumn>
{this.state.days.map(item => {
return (
<View>{item}</View>
)
})}
</PickerViewColumn>
</PickerView>
</View>
)
}
}