当前位置:首页 > VUE

vue表格实现日历

2026-01-18 14:42:40VUE

Vue 表格实现日历的方法

基础表格结构

使用 el-table 或原生 HTML 表格,通过 v-for 循环生成日历格子。月份天数通过 new Date() 计算,动态渲染表格内容。

vue表格实现日历

<template>
  <div>
    <table class="calendar-table">
      <thead>
        <tr>
          <th v-for="day in ['日', '一', '二', '三', '四', '五', '六']" :key="day">{{ day }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(week, index) in calendarData" :key="index">
          <td v-for="(date, i) in week" :key="i">{{ date || '' }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

动态生成日历数据

通过 JavaScript 计算当前月份的天数和起始星期,生成二维数组格式的日历数据。

vue表格实现日历

<script>
export default {
  data() {
    return {
      currentYear: new Date().getFullYear(),
      currentMonth: new Date().getMonth()
    }
  },
  computed: {
    calendarData() {
      const firstDay = new Date(this.currentYear, this.currentMonth, 1).getDay()
      const daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate()

      let date = 1
      const weeks = []
      for (let i = 0; i < 6; i++) {
        const week = []
        for (let j = 0; j < 7; j++) {
          if ((i === 0 && j < firstDay) || date > daysInMonth) {
            week.push(null)
          } else {
            week.push(date++)
          }
        }
        weeks.push(week)
        if (date > daysInMonth) break
      }
      return weeks
    }
  }
}
</script>

样式优化

添加 CSS 美化日历表格,使其更接近传统日历外观。

<style scoped>
.calendar-table {
  width: 100%;
  border-collapse: collapse;
}
.calendar-table th, .calendar-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: center;
}
.calendar-table td:hover {
  background-color: #f5f5f5;
}
</style>

高级功能扩展

  1. 日期点击事件:为 td 添加 @click 事件处理选中日期
  2. 月份切换:添加按钮控制 currentMonth 的增减
  3. 事件标记:在特定日期格子中通过 v-if 显示事件图标
  4. 响应式布局:通过 CSS 媒体查询适配移动端
methods: {
  handleDateClick(date) {
    if (date) {
      console.log(`选中日期: ${this.currentYear}-${this.currentMonth+1}-${date}`)
    }
  },
  changeMonth(offset) {
    this.currentMonth += offset
    // 处理年份跨年
    if (this.currentMonth < 0) {
      this.currentMonth = 11
      this.currentYear--
    } else if (this.currentMonth > 11) {
      this.currentMonth = 0
      this.currentYear++
    }
  }
}

第三方库集成

对于复杂需求,可考虑集成专用日历库:

  • FullCalendar:通过 @fullcalendar/vue 包装器使用
  • Vuetify:使用 <v-calendar> 组件快速实现
  • Element UI:结合 el-date-picker 和自定义表格
npm install @fullcalendar/vue @fullcalendar/daygrid
import FullCalendar from '@fullcalendar/vue'
import dayGridPlugin from '@fullcalendar/daygrid'

export default {
  components: {
    FullCalendar
  },
  data() {
    return {
      calendarOptions: {
        plugins: [dayGridPlugin],
        initialView: 'dayGridMonth'
      }
    }
  }
}

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

相关文章

vue实现表格复选

vue实现表格复选

Vue实现表格复选的方法 基础实现 在Vue中实现表格复选功能可以通过v-model绑定复选框的状态,结合v-for循环渲染表格数据。以下是一个基础示例: <template> &l…

vue实现导出表格

vue实现导出表格

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

使用vue实现表格

使用vue实现表格

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

css怎么制作表格

css怎么制作表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,结合CSS可以自定义样式。以下是基本实现方法: HTML结构 <table> <…

表格制作css

表格制作css

基础表格样式 使用CSS可以轻松地为HTML表格添加样式。以下是一个基础表格的HTML和CSS示例: <table class="basic-table"> <thead>…

表格tb制作css

表格tb制作css

使用CSS美化HTML表格 通过CSS可以自定义表格样式,使其更美观和符合设计需求。以下是一些常用方法: 基础表格样式 table { width: 100%; border-collap…