当前位置:首页 > VUE

vue日历表格实现

2026-01-07 04:02:56VUE

Vue 日历表格实现方法

基础日历表格结构

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

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上一月</button>
      <h2>{{ currentYear }}年{{ currentMonth + 1 }}月</h2>
      <button @click="nextMonth">下一月</button>
    </div>
    <div class="weekdays">
      <div v-for="day in weekdays" :key="day">{{ day }}</div>
    </div>
    <div class="days">
      <div 
        v-for="(date, index) in calendarDays" 
        :key="index"
        :class="{ 'other-month': !date.isCurrentMonth }"
      >
        {{ date.day }}
      </div>
    </div>
  </div>
</template>

计算日历数据

在 Vue 的 datacomputed 中定义计算逻辑,生成当前月份的日期数组,并处理跨月显示。

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

      const days = [];
      // 填充上个月末尾的日期
      for (let i = 0; i < firstDay; i++) {
        days.push({
          day: new Date(year, month, -i).getDate(),
          isCurrentMonth: false
        });
      }
      days.reverse();

      // 填充当前月日期
      for (let i = 1; i <= daysInMonth; i++) {
        days.push({
          day: i,
          isCurrentMonth: true
        });
      }

      // 填充下个月开始的日期
      const remainingCells = 42 - days.length; // 6行x7列
      for (let i = 1; i <= remainingCells; i++) {
        days.push({
          day: i,
          isCurrentMonth: false
        });
      }

      return days;
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth - 1, 1);
    },
    nextMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth + 1, 1);
    }
  }
};
</script>

样式优化

通过 CSS 美化日历表格,区分当前月份与非当前月份的日期样式。

vue日历表格实现

<style scoped>
.calendar {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.weekdays, .days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.weekdays div {
  text-align: center;
  font-weight: bold;
  padding: 10px;
}
.days div {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
  cursor: pointer;
}
.days div.other-month {
  color: #ccc;
}
</style>

高级功能扩展

为日历添加事件处理功能,例如点击日期触发事件或标记特定日期。

methods: {
  handleDateClick(date) {
    if (date.isCurrentMonth) {
      console.log(`选中日期: ${this.currentYear}-${this.currentMonth + 1}-${date.day}`);
    }
  }
}

在模板中绑定点击事件:

vue日历表格实现

<div 
  v-for="(date, index) in calendarDays" 
  :key="index"
  :class="{ 'other-month': !date.isCurrentMonth }"
  @click="handleDateClick(date)"
>
  {{ date.day }}
</div>

第三方库集成

如需更复杂功能,可集成第三方日历库如 v-calendar

npm install v-calendar

基础使用示例:

import VCalendar from 'v-calendar';
Vue.use(VCalendar);
<template>
  <v-calendar :attributes="attributes" />
</template>
<script>
export default {
  data() {
    return {
      attributes: [
        {
          key: 'today',
          dot: 'red',
          dates: new Date()
        }
      ]
    };
  }
};
</script>

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

相关文章

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现表格上传头像

vue实现表格上传头像

实现表格上传头像的功能 在Vue中实现表格上传头像功能,需要结合文件上传组件和表格组件。以下是一个完整的实现方案。 安装依赖 确保项目中安装了必要的依赖,如element-ui或ant-design…

css 制作表格

css 制作表格

基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<th>(表头)和<td>(单元格)标签。例如: <table…

vue实现导出表格

vue实现导出表格

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

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 在 Vue 中实现表格可以通过多种方式完成,以下是一个基于 Vue 3 的示例,展示如何动态渲染表格数据并支持基础功能(如排序、筛选)。 基础表格实现 通过 v-for 循环…

网页表格制作css代码

网页表格制作css代码

基础表格样式 使用CSS为HTML表格添加基础样式,如边框、间距和背景色: table { width: 100%; border-collapse: collapse; margin:…