当前位置:   article > 正文

微信小程序使用picker-view自定义时间选择器_微信小程序 picker-view

微信小程序 picker-view

timePick.vue

<template>
  <view class=" ">
    <view class="picker_view">
      <picker-view :indicator-style="indicatorStyle" :value="value" @change="bindChange" indicator-class="picker-inner">
        <picker-view-column>
          <view class="item" v-for="(item, index) in years" :key="index"
            :style="{ fontWeight: item === year ? 'bold' : 'normal' }">{{ item
            }}</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item, index) in months" :key="index"
            :style="{ fontWeight: item === month ? 'bold' : 'normal' }">{{ item }}</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item, index) in days" :key="index"
            :style="{ fontWeight: item === day ? 'bold' : 'normal' }">{{ item }}</view>
        </picker-view-column>
      </picker-view>
    </view>
  </view>
</template>

<script>
// {{year}}-{{month}}-{{day}} {{hour}}:{{minute}}
export default {
  name: 'testCom',
  // 开始年份
  props: {
    beginYear: {
      type: [String, Number],
      default() {
        return '2019'
      }
    },
    //结束年份
    endYear: {
      type: [String, Number],
      default() {
        return '2050'
      }
    },

  },
  data() {
    return {
      userSelectDate: '',
      years: [],
      year: 0,
      months: [],
      month: 0,
      days: [],
      day: 0,
      hour: 0,
      hours: [],
      minutes: [],
      minute: 0,
      nowYear: 0,
      value: [0, 0, 0],
      visible: true,
      indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth / (750 / 80))}px; background: rgba(0,0,0,0.03);font-size:16px;font-weight:700 !important;border-radius:4px;`,
      selectArr: [],
      showPicker: true,
      selectRes: ''
    };
  },
  created() {
    // console.log(this.nowYear, this.month, this.day, 'nowYear')
    this.initPicker()
  },

  mounted() {
    this.$nextTick(() => {
      this.value = [this.nowYear, this.month - 1, this.day - 1]
    })
  },

  methods: {
    // 初始化picker
    initPicker() {
      const date = new Date()
      this.year = date.getFullYear()
      this.month = date.getMonth() + 1
      this.day = date.getDate()
      const dayCount = new Date(this.year, this.month, 0).getDate()
      const hour = date.getHours()
      const minutes = []
      const minute = date.getMinutes()
      // // 当前年计算  
      this.nowYear = date.getFullYear() - this.beginYear

      for (let i = this.beginYear; i <= this.endYear; i++) {
        this.years.push(i)
      }
      for (let i = 1; i <= 12; i++) {
        this.months.push(i)
      }
      for (let i = 1; i <= dayCount; i++) {
        this.days.push(i)
      }
      for (let i = 1; i <= 24; i++) {
        this.hours.push(i)
      }
      for (let i = 1; i <= 60; i++) {
        this.minutes.push(i)
      }
    },

    // 显示picker
    change() {
      this.showPicker = true;
      // 防止第一次点击无返回值
      this.selectRes = `${this.year + '-' + this.month + '-' + this.day + ' ' + this.hour + ':' + this.minute}`;
    },
    bindChange: function (e) {
      const val = e.detail.value

      // 页面显示数值
      this.year = this.years[val[0]]
      console.log(this.year, 'this.year')
      this.month = this.months[val[1]]
      // 获取当前月份的天数
      let dayCount = new Date(this.year, this.month, 0).getDate()
      let dayArray = []
      for (let i = 1; i <= dayCount; i++) {
        dayArray.push(i)
      }
      this.days = Object.assign([], dayArray)
      this.day = this.days[val[2]]
      // 返回时间
      this.selectRes = `${this.year + '-' + this.month + '-' + this.day}`;
      this.$emit('change', this.selectRes)
    }
  }
}
</script>

<style lang="scss">
.picker-inner {
  font-weight: 700 !important;
  font-size: 16px !important;
}

.box {
  position: relative;
  z-index: 888;
}

.mask {
  position: fixed;
  z-index: 999;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s ease;

  &.show {
    visibility: visible;
    opacity: 1;
  }
}

.picker_view {
  width: 100%;
  height: 200px;
  overflow: hidden;
  background-color: rgba(255, 255, 255, 1);
  z-index: 666;

  picker-view {
    height: 100%;
  }

  .item {
    text-align: center;
    width: 100%;
    line-height: 88upx;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 30upx;
  }
}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/508199
推荐阅读
相关标签
  

闽ICP备14008679号