使用Vue写一个日期选择器

以下是一个使用Vue编写的日期选择器的示例代码

<template>
  <div>
    <input type="text" v-model="selectedDate" @click="showDatePicker" placeholder="选择日期">
    <div v-if="isDatePickerVisible" class="date-picker">
      <div class="header">
        <button @click="prevMonth"><</button>
        <span>{{ currentMonth }}</span>
        <button @click="nextMonth">></button>
      </div>
      <div class="days">
        <div v-for="day in daysInMonth" :key="day" class="day" @click="selectDate(day)">
          {{ day }}
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      selectedDate: '',
      currentMonth: '',
      isDatePickerVisible: false
    };
  },
  computed: {
    daysInMonth() {
      const date = new Date(this.currentMonth);
      const year = date.getFullYear();
      const month = date.getMonth();
      const daysInMonth = new Date(year, month + 1, 0).getDate();
      return Array.from({ length: daysInMonth }, (_, index) => index + 1);
    }
  },
  methods: {
    showDatePicker() {
      this.isDatePickerVisible = true;
      this.currentMonth = new Date();
    },
    prevMonth() {
      const prevMonth = new Date(this.currentMonth);
      prevMonth.setMonth(prevMonth.getMonth() - 1);
      this.currentMonth = prevMonth;
    },
    nextMonth() {
      const nextMonth = new Date(this.currentMonth);
      nextMonth.setMonth(nextMonth.getMonth() + 1);
      this.currentMonth = nextMonth;
    },
    selectDate(day) {
      const selectedDate = new Date(this.currentMonth);
      selectedDate.setDate(day);
      const formattedDate = selectedDate.toLocaleDateString('en-US');
      this.selectedDate = formattedDate;
      this.isDatePickerVisible = false;
    }
  }
};
</script>
 
<style scoped>
.date-picker {
  position: absolute;
  top: 40px;
  left: 0;
  background: #fff;
  border: 1px solid #ccc;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 10px;
}
 
.header {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
}
 
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
}
 
.day {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  height: 40px;
  border: 1px solid #ccc;
}
 
input {
  padding: 5px;
  border: 1px solid #ccc;
}
</style>

在上述代码中,我们使用了一个input元素来显示和输入选中的日期,并使用一个div元素作为日期选择器的容器。当点击input元素时,会显示日期选择器。日期选择器的实现参考了当前月份的日历,并提供了上一个月和下一个月的按钮来切换月份。

在Vue组件中,我们使用了data属性来定义必要的数据,如选中的日期、当前月份,以及一个表示日期选择器是否可见的标志。computed属性用于计算当前月份的天数,并将其渲染到日期选择器中。methods属性定义了一些用于展示和操作日期选择器的方法。

通过上述代码,您可以将日期选择器组件集成到您的Vue应用程序中,并使用v-model指令来进行双向数据绑定,以获取选中的日期。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Vue 3 一个出生日期选择器的示例代码: ```html <template> <div> <label for="year">年:</label> <select id="year" v-model="selectedYear" @change="updateDays"> <option v-for="year in years" :key="year" :value="year">{{ year }} 年</option> </select> <label for="month">月:</label> <select id="month" v-model="selectedMonth" @change="updateDays"> <option v-for="(month, index) in months" :key="index" :value="index + 1">{{ month }} 月</option> </select> <label for="day">日:</label> <select id="day" v-model="selectedDay"> <option v-for="day in days" :key="day" :value="day">{{ day }} 日</option> </select> </div> </template> <script> export default { data() { return { selectedYear: null, selectedMonth: null, selectedDay: null, years: [], months: [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ], days: [] } }, mounted() { // 初始化年份 const currentYear = new Date().getFullYear() for (let i = currentYear; i >= currentYear - 100; i--) { this.years.push(i) } // 初始化月份和天数 this.selectedMonth = new Date().getMonth() + 1 this.updateDays() }, methods: { updateDays() { if (this.selectedYear && this.selectedMonth) { // 计算选中月份的天数 const daysInMonth = new Date(this.selectedYear, this.selectedMonth, 0).getDate() // 更新可选天数 this.days = [] for (let i = 1; i <= daysInMonth; i++) { this.days.push(i) } // 如果选中的天数超出当前月份的最大天数,重置选中天数 if (this.selectedDay > daysInMonth) { this.selectedDay = null } } } } } </script> ``` 这个出生日期选择器包含了年、月、日三个下拉框,用户可以通过选择这三个下拉框来选择自己的出生日期。其中,年份的范围是当前年份往前100年,月份和天数会根据选中的年份和月份动态更新可选范围,以保证选择的日期是合法的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值