当前位置:首页 > VUE

vue日历表格实现

2026-02-09 17:05:42VUE

Vue 日历表格实现方法

基础日历表格结构

使用 Vue 的模板语法构建日历表格框架,通过 v-for 循环生成日期格子。核心是计算当前月份的天数及起始星期位置。

<template>
  <div class="calendar">
    <div class="header">
      <span v-for="day in weekDays" :key="day">{{ day }}</span>
    </div>
    <div class="body">
      <div 
        v-for="date in visibleDates" 
        :key="date.id"
        :class="{ 'other-month': !date.isCurrentMonth }"
      >
        {{ date.day }}
      </div>
    </div>
  </div>
</template>

日期计算逻辑

在 Vue 的 datacomputed 中定义日期生成方法,需处理跨月显示的日期(上个月末和下个月初的日期)。

export default {
  data() {
    return {
      currentDate: new Date(),
      weekDays: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    visibleDates() {
      const year = this.currentDate.getFullYear();
      const month = this.currentDate.getMonth();
      const firstDay = new Date(year, month, 1).getDay();
      const daysInMonth = new Date(year, month + 1, 0).getDate();

      // 生成当前月日期
      const dates = Array.from({ length: daysInMonth }, (_, i) => ({
        day: i + 1,
        isCurrentMonth: true
      }));

      // 补全上月剩余日期
      const prevMonthDays = firstDay;
      for (let i = 0; i < prevMonthDays; i++) {
        dates.unshift({
          day: new Date(year, month, -i).getDate(),
          isCurrentMonth: false
        });
      }

      // 补全下月起始日期(确保6行显示)
      const totalCells = Math.ceil(dates.length / 7) * 7;
      const nextMonthDays = totalCells - dates.length;
      for (let i = 1; i <= nextMonthDays; i++) {
        dates.push({
          day: i,
          isCurrentMonth: false
        });
      }

      return dates;
    }
  }
}

样式布局

使用 CSS Grid 或 Flexbox 实现日历的网格布局,注意区分当前月与其他月的样式差异。

.calendar {
  width: 100%;
  max-width: 600px;
}

.header {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-weight: bold;
}

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

.body div {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
}

.other-month {
  color: #ccc;
}

交互功能扩展

增加月份切换和日期点击事件,通过方法修改 currentDate 实现动态更新。

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
    );
  },
  handleDateClick(date) {
    if (date.isCurrentMonth) {
      console.log('Selected date:', date.day);
    }
  }
}

使用第三方库(可选)

对于复杂需求(如拖拽事件、农历显示),可集成专用日历库:

vue日历表格实现

  • Vue2: v-calendar
  • Vue3: @vuepic/vue-datepicker 安装后通过组件化方式快速实现:
import { VCalendar } from 'v-calendar';
export default {
  components: { VCalendar }
}

标签: 表格日历
分享给朋友:

相关文章

vue实现全年日历

vue实现全年日历

实现全年日历的基本思路 使用Vue实现全年日历需要结合日期计算、数据渲染和交互逻辑。核心在于生成全年12个月的日历数据,并通过组件化方式展示。以下是具体实现方法: 安装依赖(可选) 如需更便捷的日期…

vue实现日历组件

vue实现日历组件

Vue 日历组件实现步骤 基础结构搭建 使用Vue 3的Composition API或Options API创建组件框架。核心结构包括年份/月份切换按钮、星期标题栏和日期网格。 <t…

vue 实现表格渲染

vue 实现表格渲染

基本表格渲染 在 Vue 中渲染表格通常使用 v-for 指令遍历数据数组,动态生成表格行。以下是一个简单的示例: <template> <table> <…

vue实现拖拽表格

vue实现拖拽表格

Vue 实现拖拽表格的方法 使用第三方库(推荐) 推荐使用 vuedraggable 或 sortablejs 这类成熟的拖拽库,它们对 Vue 有良好支持且功能丰富。 安装 vuedraggabl…

vue实现mac日历

vue实现mac日历

Vue 实现 Mac 日历功能 要在 Vue 中实现类似 Mac 日历的功能,可以结合第三方库或自定义组件开发。以下是两种常见方法: 使用 FullCalendar 库 FullCalendar 是…

vue实现日历效果

vue实现日历效果

Vue实现日历效果的方法 使用第三方库 Vue生态中有多个成熟的日历组件库,如v-calendar或vue2-calendar,可以快速实现日历功能。安装后直接引入组件即可使用。 npm insta…