当前位置:首页 > VUE

实现vue datepicker

2026-01-12 23:43:00VUE

以下是实现 Vue Datepicker 的几种方法和步骤:

使用第三方库(如 vue-datepicker)

安装 vue-datepicker 库:

npm install vue-datepicker --save

在 Vue 组件中引入并使用:

<template>
  <datepicker v-model="selectedDate"></datepicker>
</template>

<script>
import Datepicker from 'vue-datepicker';

export default {
  components: { Datepicker },
  data() {
    return {
      selectedDate: new Date()
    };
  }
};
</script>

自定义基础 Datepicker 组件

创建自定义 Datepicker 组件:

<template>
  <div class="datepicker">
    <input 
      type="text" 
      v-model="displayDate" 
      @focus="showCalendar = true"
      @blur="handleBlur"
    >
    <div class="calendar" v-if="showCalendar">
      <div class="calendar-header">
        <button @click="prevMonth">←</button>
        <span>{{ currentMonth }} {{ currentYear }}</span>
        <button @click="nextMonth">→</button>
      </div>
      <div class="calendar-grid">
        <div v-for="day in days" :key="day" class="calendar-day">
          {{ day }}
        </div>
        <div 
          v-for="date in visibleDates" 
          :key="date.getTime()"
          class="calendar-date"
          :class="{ 'selected': isSelected(date) }"
          @click="selectDate(date)"
        >
          {{ date.getDate() }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDate: null,
      displayDate: '',
      showCalendar: false,
      currentDate: new Date(),
      days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
    };
  },
  computed: {
    currentMonth() {
      return this.currentDate.toLocaleString('default', { month: 'long' });
    },
    currentYear() {
      return this.currentDate.getFullYear();
    },
    visibleDates() {
      const year = this.currentDate.getFullYear();
      const month = this.currentDate.getMonth();
      const firstDay = new Date(year, month, 1);
      const lastDay = new Date(year, month + 1, 0);

      let dates = [];
      for (let i = 1; i <= lastDay.getDate(); i++) {
        dates.push(new Date(year, month, i));
      }

      return dates;
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() - 1,
        1
      );
    },
    nextMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() + 1,
        1
      );
    },
    selectDate(date) {
      this.selectedDate = date;
      this.displayDate = date.toLocaleDateString();
      this.showCalendar = false;
    },
    isSelected(date) {
      return this.selectedDate && 
        date.toDateString() === this.selectedDate.toDateString();
    },
    handleBlur() {
      setTimeout(() => {
        this.showCalendar = false;
      }, 200);
    }
  }
};
</script>

<style>
.datepicker {
  position: relative;
  display: inline-block;
}

.calendar {
  position: absolute;
  border: 1px solid #ddd;
  padding: 10px;
  background: white;
  z-index: 1000;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.calendar-day, .calendar-date {
  text-align: center;
  padding: 5px;
}

.calendar-date:hover {
  background-color: #eee;
  cursor: pointer;
}

.selected {
  background-color: #4285f4;
  color: white;
}
</style>

使用 Vuetify 的日期选择器

安装 Vuetify:

vue add vuetify

使用 Vuetify 的日期选择器组件:

<template>
  <v-app>
    <v-container>
      <v-date-picker v-model="date"></v-date-picker>
    </v-container>
  </v-app>
</template>

<script>
export default {
  data: () => ({
    date: new Date().toISOString().substr(0, 10)
  })
};
</script>

使用 Element UI 的日期选择器

安装 Element UI:

npm install element-ui -S

在 main.js 中引入:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

在组件中使用:

实现vue datepicker

<template>
  <div>
    <el-date-picker v-model="date" type="date"></el-date-picker>
  </div>
</template>

<script>
export default {
  data() {
    return {
      date: ''
    };
  }
};
</script>

每种方法都有其适用场景:第三方库适合快速实现,自定义组件适合特定需求,UI 框架集成适合已有项目架构。根据项目需求选择最合适的方法。

标签: vuedatepicker
分享给朋友:

相关文章

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双向…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="con…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…