当前位置:首页 > 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);
    }
  }
}

使用第三方库(可选)

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

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

vue日历表格实现

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

相关文章

vue实现导出表格

vue实现导出表格

Vue 中实现表格导出 使用 xlsx 库导出 Excel 安装 xlsx 库: npm install xlsx --save 在 Vue 组件中引入并使用: import XLSX fro…

vue表格实现复选框

vue表格实现复选框

Vue表格实现复选框的方法 基础实现(使用v-model绑定数组) 在Vue中可以通过v-model绑定数组来实现复选框功能。以下是一个基础示例: <template> <t…

uniapp 表格导入

uniapp 表格导入

uniapp 表格导入的实现方法 使用 uni.chooseFile 选择文件 在 uniapp 中,可以通过 uni.chooseFile API 让用户选择本地文件。该 API 支持多平台,包括…

vue实现表格导入

vue实现表格导入

Vue 实现表格导入的方法 使用 Element UI 的 Upload 组件 Element UI 提供了 el-upload 组件,可以方便地实现文件上传功能。结合 xlsx 库解析 Excel…

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…

vue实现分组表格

vue实现分组表格

Vue 实现分组表格的方法 使用 v-for 嵌套循环实现分组 通过嵌套的 v-for 循环可以轻松实现分组表格。外层循环遍历分组数据,内层循环遍历每组中的具体项。 <template>…