PickerView

嵌入页面的滚动选择器 其中只可放置 picker-view-column 组件,其它节点不会显示

参考文档

类型

  1. ComponentType<PickerViewProps>

示例代码

  • React
  • Vue
  1. export default class Picks extends Component {
  2. constructor () {
  3. super(...arguments)
  4. const date = new Date()
  5. const years = []
  6. const months = []
  7. const days = []
  8. for (let i = 1990; i <= date.getFullYear(); i++) {
  9. years.push(i)
  10. }
  11. for (let i = 1; i <= 12; i++) {
  12. months.push(i)
  13. }
  14. for (let i = 1; i <= 31; i++) {
  15. days.push(i)
  16. }
  17. this.state = {
  18. years: years,
  19. year: date.getFullYear(),
  20. months: months,
  21. month: 2,
  22. days: days,
  23. day: 2,
  24. value: [9999, 1, 1]
  25. }
  26. }
  27. onChange = e => {
  28. const val = e.detail.value
  29. this.setState({
  30. year: this.state.years[val[0]],
  31. month: this.state.months[val[1]],
  32. day: this.state.days[val[2]],
  33. value: val
  34. })
  35. }
  36. render() {
  37. return (
  38. <View>
  39. <View>{this.state.year}年{this.state.month}月{this.state.day}日</View>
  40. <PickerView indicatorStyle='height: 50px;' style='width: 100%; height: 300px;' value={this.state.value} onChange={this.onChange}>
  41. <PickerViewColumn>
  42. {this.state.years.map(item => {
  43. return (
  44. <View>{item}年</View>
  45. );
  46. })}
  47. </PickerViewColumn>
  48. <PickerViewColumn>
  49. {this.state.months.map(item => {
  50. return (
  51. <View>{item}月</View>
  52. )
  53. })}
  54. </PickerViewColumn>
  55. <PickerViewColumn>
  56. {this.state.days.map(item => {
  57. return (
  58. <View>{item}日</View>
  59. )
  60. })}
  61. </PickerViewColumn>
  62. </PickerView>
  63. </View>
  64. )
  65. }
  66. }
  1. <template>
  2. <view class="taro-example">
  3. <view>{{year}}年{{month}}月{{day}}日</view>
  4. <picker-view indicator-style="height: 30px;" style="width: 100%; height: 300px;" :value="value" @change="onChange">
  5. <picker-view-column>
  6. <view v-for="(item, index) in years" :key="index">{{item}}年</view>
  7. </picker-view-column>
  8. <picker-view-column>
  9. <view v-for="(item, index) in months" :key="index">{{item}}月</view>
  10. </picker-view-column>
  11. <picker-view-column>
  12. <view v-for="(item, index) in days" :key="index">{{item}}日</view>
  13. </picker-view-column>
  14. </picker-view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: "Index",
  20. data() {
  21. const date = new Date()
  22. const years = []
  23. const months = []
  24. const days = []
  25. for (let i = 1990; i <= date.getFullYear(); i++) {
  26. years.push(i)
  27. }
  28. for (let i = 1; i <= 12; i++) {
  29. months.push(i)
  30. }
  31. for (let i = 1; i <= 31; i++) {
  32. days.push(i)
  33. }
  34. return {
  35. years: years,
  36. year: date.getFullYear(),
  37. months: months,
  38. month: 2,
  39. days: days,
  40. day: 2,
  41. value: [3, 1, 1]
  42. }
  43. },
  44. methods: {
  45. onChange: function(e) {
  46. const val = e.detail.value
  47. console.log(val)
  48. this.year = this.years[val[0]]
  49. this.month = this.months[val[1]]
  50. this.day = this.days[val[2]]
  51. }
  52. }
  53. }
  54. </script>

PickerViewProps

参数类型必填说明
valuenumber[]数组中的数字依次表示 picker-view 内的 picker-view-column 选择的第几项(下标从 0 开始),数字大于 picker-view-column 可选项长度时,选择最后一项。
indicatorStylestring设置选择器中间选中框的样式
indicatorClassstring设置选择器中间选中框的类名
maskStylestring设置蒙层的样式
maskClassstring设置蒙层的类名
onChangeBaseEventOrigFunction<onChangeEventDetail>当滚动选择,value 改变时触发 change 事件,event.detail = {value: value};value为数组,表示 picker-view 内的 picker-view-column 当前选择的是第几项(下标从 0 开始)
onPickStartBaseEventOrigFunction<any>当滚动选择开始时候触发事件
onPickEndBaseEventOrigFunction<any>当滚动选择结束时候触发事件

API 支持度

API微信小程序百度小程序支付宝小程序字节跳动小程序H5React Native
PickerViewProps.value✔️✔️✔️✔️
PickerViewProps.indicatorStyle✔️✔️✔️✔️
PickerViewProps.indicatorClass✔️✔️✔️
PickerViewProps.maskStyle✔️✔️✔️✔️
PickerViewProps.maskClass✔️✔️✔️
PickerViewProps.onChange✔️✔️✔️✔️
PickerViewProps.onPickStart✔️
PickerViewProps.onPickEnd✔️

onChangeEventDetail

参数类型
valuenumber[]

API 支持度

API微信小程序百度小程序支付宝小程序字节跳动小程序H5React Native
PickerView✔️✔️✔️✔️✔️