当前位置:首页 > 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 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition 组件…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面: me…

vue实现图片查看

vue实现图片查看

Vue 实现图片查看功能 在 Vue 中实现图片查看功能,可以通过原生实现或使用第三方库。以下是几种常见的方法: 原生实现方式 使用 Vue 的指令和事件绑定实现基础的图片查看功能。 <te…