uniapp vue 前端页面半小时选择器

2024-01-08 11:29:01
<template>
  <view>
    <view class="uni-title uni-common-pl">时间选择器</view>
    <view class="uni-list">
      <view class="uni-list-cell">
        <view class="uni-list-cell-left">
          当前选择
        </view>
        <view class="uni-list-cell-db">
          <picker mode="selector" :value="timeIndex" range="{{timeRange}}" @change="bindTimeChange">
            <view class="uni-input">{{time}}</view>
          </picker>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      time: '', // 当前选择的时间
      timeIndex: 0, // 当前选择的时间索引
      timeRange: [] // 时间选择器的可选项
    };
  },
  mounted() {
    this.generateTimeRange();
  },
  methods: {
    generateTimeRange() {
      const startTime = new Date("2000/01/01 08:00");
      const endTime = new Date("2000/01/01 22:00");

      const timeOptions = [];
      while (startTime <= endTime) {
        const hour = String(startTime.getHours()).padStart(2, '0');
        const minute = String(startTime.getMinutes()).padStart(2, '0');
        const timeOption = `${hour}:${minute}`;
        timeOptions.push(timeOption);

        startTime.setMinutes(startTime.getMinutes() + 30);
      }

      this.timeRange = timeOptions;
      this.time = timeOptions[0];
    },
    bindTimeChange(e) {
      const selectedTimeIndex = e.mp.detail.value;
      this.timeIndex = selectedTimeIndex;
      this.time = this.timeRange[selectedTimeIndex];
    }
  }
};
</script>

文章来源:https://blog.csdn.net/qq_43193513/article/details/135381786
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。