当前位置:首页 > VUE

vue表格实现日历

2026-02-19 07:05:36VUE

Vue 表格实现日历的方法

使用 Vue 结合表格实现日历功能,可以通过动态生成表格单元格的方式展示日期。以下是一种常见的实现方案:

基础日历结构

创建 Vue 组件,使用 <table> 元素构建日历框架。需要计算当前月份的天数及起始星期:

<template>
  <div class="calendar">
    <table>
      <thead>
        <tr>
          <th v-for="day in weekDays" :key="day">{{ day }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(week, index) in weeks" :key="index">
          <td v-for="(date, i) in week" :key="i">
            {{ date.getDate() }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      weekDays: ['日', '一', '二', '三', '四', '五', '六'],
      currentDate: new Date()
    }
  },
  computed: {
    weeks() {
      const year = this.currentDate.getFullYear()
      const month = this.currentDate.getMonth()
      const firstDay = new Date(year, month, 1)
      const lastDay = new Date(year, month + 1, 0)

      let days = []
      // 填充上月剩余天数
      for (let i = 0; i < firstDay.getDay(); i++) {
        days.push(new Date(year, month, -i))
      }
      // 当月天数
      for (let i = 1; i <= lastDay.getDate(); i++) {
        days.push(new Date(year, month, i))
      }
      // 填充下月天数
      const remaining = 7 - (days.length % 7)
      for (let i = 1; i <= remaining; i++) {
        days.push(new Date(year, month + 1, i))
      }

      // 按周分组
      let weeks = []
      for (let i = 0; i < days.length; i += 7) {
        weeks.push(days.slice(i, i + 7))
      }
      return weeks
    }
  }
}
</script>

添加交互功能

为日历添加月份切换和日期选择功能:

<template>
  <div>
    <button @click="prevMonth">上个月</button>
    <span>{{ currentDate.getFullYear() }}年{{ currentDate.getMonth() + 1 }}月</span>
    <button @click="nextMonth">下个月</button>

    <table>
      <!-- ...表格结构同上... -->
          <td 
            v-for="(date, i) in week" 
            :key="i"
            :class="{
              'current-month': date.getMonth() === currentDate.getMonth(),
              'selected': selectedDate && date.toDateString() === selectedDate.toDateString()
            }"
            @click="selectDate(date)"
          >
            {{ date.getDate() }}
          </td>
      <!-- ... -->
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDate: null
    }
  },
  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
      )
    },
    selectDate(date) {
      this.selectedDate = date
    }
  }
}
</script>

样式优化

添加 CSS 美化日历外观:

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

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 10px;
  text-align: center;
  border: 1px solid #ddd;
}

th {
  background-color: #f5f5f5;
}

td.current-month {
  background-color: white;
}

td:not(.current-month) {
  color: #ccc;
}

td.selected {
  background-color: #1890ff;
  color: white;
}

高级功能扩展

  1. 事件标记:在特定日期显示标记点

    <td v-for="(date, i) in week" :key="i">
    <div class="date-cell">
     {{ date.getDate() }}
     <span v-if="hasEvent(date)" class="event-dot"></span>
    </div>
    </td>
  2. 周视图切换:添加周视图模式

    data() {
    return {
     viewMode: 'month' // 或 'week'
    }
    },
    computed: {
    weeks() {
     if (this.viewMode === 'week') {
       // 返回当周数据
     } else {
       // 返回整月数据
     }
    }
    }
  3. 拖拽功能:实现事件拖拽到不同日期

    vue表格实现日历

    <td 
    @dragover.prevent
    @drop="handleDrop(date)"
    >
    <!-- 单元格内容 -->
    </td>

通过以上方法,可以构建一个功能完善的 Vue 表格日历组件,根据需求可进一步扩展功能。

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

相关文章

vue表格组件实现

vue表格组件实现

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

vue表格动态实现

vue表格动态实现

Vue 表格动态实现方法 动态表格的实现通常涉及数据绑定、列配置动态化以及交互功能的灵活处理。以下是几种常见场景的具体实现方式: 动态数据绑定 通过v-for指令循环渲染表格行数据,数据变化时表格自…

vue实现表格刷新

vue实现表格刷新

Vue 实现表格刷新的方法 使用 v-if 强制重新渲染 通过 v-if 控制表格的显示与隐藏,切换时 Vue 会重新渲染组件。 <template> <button @cli…

vue实现表格序号

vue实现表格序号

实现表格序号的常见方法 在Vue中为表格添加序号列可以通过多种方式实现,以下是几种典型方案: 使用v-for索引 <template> <table> <…

vue实现表格穿梭

vue实现表格穿梭

Vue实现表格穿梭功能 表格穿梭功能通常指在两个表格之间进行数据转移,常见于权限管理、数据筛选等场景。以下是基于Vue的实现方法: 核心实现思路 创建两个表格组件,分别代表源数据和目标数据 使用v-…

vue 实现表格分页

vue 实现表格分页

Vue 实现表格分页的方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,结合 el-table 可以快速实现表格分页功能。 安装…