picker-view.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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" :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. }
  56. },
  57. methods: {
  58. bindChange (e) {
  59. const val = e.detail.value
  60. this.year = this.years[val[0]]
  61. this.month = this.months[val[1]]
  62. this.day = this.days[val[2]]
  63. }
  64. }
  65. }
  66. </script>
  67. <style>
  68. picker-view {
  69. width: 100%;
  70. height: 600rpx;
  71. margin-top:20rpx;
  72. }
  73. .item {
  74. line-height: 100rpx;
  75. text-align: center;
  76. }
  77. </style>