微信小程序使用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>

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序的原生 picker-view 是一种可以进行滚动选择的组件。它可以在小程序中展示一个滚动的选择器,用户可以通过上下滑动来选择需要的选项。 使用 picker-view 的步骤如下: 1. 在 wxml 文件中,通过<picker-view>标签来定义一个 picker-view 组件,可以通过style属性来设置它的样式。 2. 在 js 文件中,首先需要通过页面的setData()方法来初始化 picker-view 组件的数据源,使用数组的形式来表示选项。 3. 在 wxml 文件中,通过<picker-view-column>标签来定义 picker-view 中的每一列。可以使用wx:for属性来循环渲染每一列的选项。 4. 在 js 文件中,可以通过 onLoad()或者onShow()等生命周期方法来初始化 picker-view 组件的选中项。可以通过setData()方法更新 selected 属性的值来改变选中项。 5. 在 wxml 文件中,通过<view>标签来定义一个按钮或者其他交互元素,通过bindtap属性来绑定事件处理函数。 6. 在 js 文件中,实现事件处理函数,并通过setData()方法更新 selected 值以改变选中项。 7. 可以通过onPickerChange()等事件来监听 picker-view 的选中项改变的事件,并实现相应的逻辑。 需要注意的是,picker-view 组件的数组类型数据源,每一项都需要有value和text两个属性,分别表示选项的值和显示文本。 通过以上步骤,就可以在微信小程序使用原生 picker-view 组件实现选择器的功能。用户可以滚动选择选项,并通过自定义的事件处理函数来获取选中项的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值