使用Vue写一个日期选择器

简介: 使用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指令来进行双向数据绑定,以获取选中的日期。

相关文章
|
23天前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
159 2
|
4月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
605 0
|
6月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
812 4
|
4月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
5月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
571 77
|
3月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
128 0
|
3月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
272 1
|
6月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
4月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
388 17