picker-view.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-title">
  6. 日期:{{year}}年{{month}}月{{day}}日
  7. </view>
  8. </view>
  9. <picker-view v-if="visible" :indicator-style="indicatorStyle" :mask-style="maskStyle" :value="value" @change="bindChange">
  10. <picker-view-column>
  11. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  12. </picker-view-column>
  13. <picker-view-column>
  14. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  15. </picker-view-column>
  16. <picker-view-column>
  17. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  18. </picker-view-column>
  19. </picker-view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. data () {
  25. const date = new Date()
  26. const years = []
  27. const year = date.getFullYear()
  28. const months = []
  29. const month = date.getMonth() + 1
  30. const days = []
  31. const day = date.getDate()
  32. for (let i = 1990; i <= date.getFullYear(); i++) {
  33. years.push(i)
  34. }
  35. for (let i = 1; i <= 12; i++) {
  36. months.push(i)
  37. }
  38. for (let i = 1; i <= 31; i++) {
  39. days.push(i)
  40. }
  41. return {
  42. title: 'picker-view',
  43. years,
  44. year,
  45. months,
  46. month,
  47. days,
  48. day,
  49. value: [9999, month - 1, day - 1],
  50. /**
  51. * 解决动态设置indicator-style不生效的问题
  52. */
  53. visible: true,
  54. // indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`
  55. indicatorStyle: `height: 50px;`,
  56. // #ifdef MP-KUAISHOU
  57. maskStyle: "padding:10px 0"
  58. // #endif
  59. // #ifndef MP-KUAISHOU
  60. maskStyle: ""
  61. // #endif
  62. }
  63. },
  64. methods: {
  65. bindChange (e) {
  66. const val = e.detail.value
  67. this.year = this.years[val[0]]
  68. this.month = this.months[val[1]]
  69. this.day = this.days[val[2]]
  70. }
  71. }
  72. }
  73. </script>
  74. <style>
  75. picker-view {
  76. width: 100%;
  77. height: 600rpx;
  78. margin-top:20rpx;
  79. }
  80. .item {
  81. line-height: 100rpx;
  82. text-align: center;
  83. }
  84. </style>