当前位置:首页 > 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 组件:

实现vue 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 的日期选择器组件:

实现vue datepicker

<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);

在组件中使用:

<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中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <temp…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <tem…

vue实现多选

vue实现多选

Vue 实现多选功能 在 Vue 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组…

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 pu…

vue 实现赋值

vue 实现赋值

Vue 实现赋值的方法 在 Vue 中,赋值操作通常涉及数据绑定和响应式更新。以下是几种常见的赋值方式: 直接赋值给 data 属性 在 Vue 实例的 data 对象中定义的属性可以直接通过 th…