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

添加交互功能

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

vue表格实现日历

<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. 事件标记:在特定日期显示标记点

    vue表格实现日历

    <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. 拖拽功能:实现事件拖拽到不同日期

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

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

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

相关文章

css好看的表格制作

css好看的表格制作

CSS表格美化方法 边框与间距优化 使用border-collapse合并边框,避免双线边框。设置padding增加单元格内边距,提升可读性。 table { border-collapse:…

vue表格实现复选框

vue表格实现复选框

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

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

css表格制作

css表格制作

CSS表格制作方法 使用CSS创建表格可以通过多种方式实现,以下是常见的几种方法: 使用HTML原生表格标签结合CSS样式 <table class="styled-table">…

uniapp日历视图

uniapp日历视图

uniapp日历视图实现方法 在uniapp中实现日历视图可以通过多种方式完成,包括使用第三方组件库或自行开发。以下是几种常见方法: 使用uni-calendar组件 uni-calendar…

vue动态表格实现

vue动态表格实现

Vue动态表格实现方法 使用v-for指令渲染动态数据 通过v-for指令可以循环遍历数组数据生成表格行,数据变化时表格自动更新。示例代码: <template> <tabl…