当前位置:首页 > 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中实现表格复选框功能,可以通过以下方法完成: 方法一:使用el-table(Element UI) 安装Element UI后,在表格列中直接添加type="sele…

vue实现表格上传头像

vue实现表格上传头像

实现表格上传头像功能 在Vue中实现表格上传头像功能,可以通过以下步骤完成。这里假设使用Element UI作为UI框架,但方法同样适用于其他UI库或原生实现。 安装必要依赖 确保项目中已安装Ele…

vue表格实现单选框

vue表格实现单选框

Vue 表格实现单选框的方法 在 Vue 中实现表格单选框功能,可以通过结合 el-table(Element UI)或原生表格与 Vue 的数据绑定特性完成。以下是几种常见实现方式: 使用 Ele…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前年…

vue表格实现单选

vue表格实现单选

实现 Vue 表格单选功能 使用 v-model 绑定选中状态 在表格的每一行添加单选按钮或点击行触发选中,通过 v-model 绑定一个变量存储当前选中行的唯一标识(如 id)。 示例代码: &l…

网页表格制作css代码

网页表格制作css代码

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