当前位置:首页 > VUE

vue datepicker 实现

2026-01-12 09:53:14VUE

Vue Datepicker 实现方法

使用第三方库(推荐)

安装 vue-datepickerv-calendar 等流行库,例如:

npm install vuejs-datepicker

在组件中引入并使用:

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

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

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

自定义实现基础 Datepicker

创建可重用的 Datepicker 组件:

<template>
  <div class="datepicker">
    <input 
      type="text" 
      v-model="displayDate"
      @focus="showPicker = true"
      @blur="handleBlur"
    >
    <div class="calendar" v-if="showPicker">
      <!-- 日历头部 -->
      <div class="header">
        <button @click="prevMonth">←</button>
        <span>{{ currentMonth }} {{ currentYear }}</span>
        <button @click="nextMonth">→</button>
      </div>

      <!-- 日历内容 -->
      <div class="days">
        <div v-for="day in days" :key="day" class="day">
          {{ day }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDate: null,
      displayDate: '',
      showPicker: false,
      currentDate: new Date(),
      days: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentMonth() {
      return this.currentDate.getMonth() + 1
    },
    currentYear() {
      return this.currentDate.getFullYear()
    }
  },
  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
      )
    },
    handleBlur() {
      setTimeout(() => {
        this.showPicker = false
      }, 200)
    }
  }
}
</script>

<style>
.datepicker {
  position: relative;
}
.calendar {
  position: absolute;
  border: 1px solid #ccc;
  padding: 10px;
  background: white;
  z-index: 100;
}
</style>

高级功能扩展

为自定义 Datepicker 添加更多功能:

// 在 methods 中添加日期选择方法
selectDate(date) {
  this.selectedDate = date
  this.displayDate = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`
  this.showPicker = false
}

// 计算当月天数
getDaysInMonth() {
  return new Date(
    this.currentDate.getFullYear(),
    this.currentDate.getMonth() + 1,
    0
  ).getDate()
}

// 生成日期单元格
generateDates() {
  const daysInMonth = this.getDaysInMonth()
  const dates = []
  for (let i = 1; i <= daysInMonth; i++) {
    dates.push(new Date(
      this.currentDate.getFullYear(),
      this.currentDate.getMonth(),
      i
    ))
  }
  return dates
}

国际化支持

添加多语言支持:

data() {
  return {
    locales: {
      en: {
        days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
      },
      zh: {
        days: ['日', '一', '二', '三', '四', '五', '六'],
        months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
      }
    },
    currentLocale: 'zh'
  }
}

日期范围限制

添加 minDate 和 maxDate 限制:

vue datepicker 实现

props: {
  minDate: {
    type: Date,
    default: null
  },
  maxDate: {
    type: Date,
    default: null
  }
},
methods: {
  isDateDisabled(date) {
    if (this.minDate && date < this.minDate) return true
    if (this.maxDate && date > this.maxDate) return true
    return false
  }
}

标签: vuedatepicker
分享给朋友:

相关文章

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件:…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…

vue实现字幕

vue实现字幕

Vue 实现字幕功能 在 Vue 中实现字幕功能可以通过动态绑定数据和 CSS 动画来实现。以下是具体实现方法: 1. 基础字幕组件 创建一个字幕组件,通过 v-model 或 props 接收字…

vue实现梯形

vue实现梯形

在Vue中实现梯形效果,可以通过CSS的transform属性或clip-path属性来实现。以下是几种常见的实现方法: 使用CSS transform实现梯形 通过CSS的transform: s…