自定义日历组件

最近vue项目需要写一个日历组件;
涉及到渲染数据的生成。

生成渲染数据的方法如下:

buildAllYearCalendarDayRows() {
    this.allYearCalendarDayRows = [];
    let allYearCalendarDayRows = [];
    let year = this.calendarFilter.year;
    for (let month = 1; month <= 12; month++) { // 12个月
        let startOfMonth = moment(`${year}-${month}`).startOf('month');
        let start = null;
        if(startOfMonth.day()===0){
            start = startOfMonth.subtract(1, 'day').weekday(1).subtract(1, 'day');
        }else{
            start = startOfMonth.weekday(1).subtract(1, 'day');
        }
        let monthCalendarDayRows = [];
        for (let rowIndex = 0; rowIndex < 6; rowIndex++) { // 一个月6周
            monthCalendarDayRows[rowIndex] = [];
            for (let i = 0; i < 7; i++) { // 一周7天
                start = start.add(1, 'day');
                monthCalendarDayRows[rowIndex].push({dayString:start.format('DD'), dayMoment:start, isCurrentMonth: start.month() === (month-1), isWorkDay: start.day()<6})
            }
        }
        allYearCalendarDayRows.push(monthCalendarDayRows);
    }
    this.$set(this, 'allYearCalendarDayRows', allYearCalendarDayRows);
}

生成的数据如下

在这里插入图片描述

定义组件:

Vue.component('calendar', {
                template: `
                <div class="el-calendar">
                    <div class="el-calendar__header">
                        <slot name="header" :month="month"></slot>
                    </div>
                    <table class="el-calendar-table" cellspacing="0" cellpadding="0">
                        <thead>
                        <th v-for="(v, k) of weeks" :key="k">{{ v }}</th>
                        </thead>
                        <tbody>
                        <tr v-for="(dayRow, k) of month_calendar_day_rows" :key="k">
                            <td v-for="(day, kk) of dayRow" :key="kk" >
                                <slot name="cell" :day="day">{{day}}</slot>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                `,
                props: {
                    weeks: {
                        type: Array,
                        default: ['一', '二', '三', '四', '五', '六', '七']
                    },
                    month: {
                        type: Number,
                        default: 1
                    },
                    month_calendar_day_rows: {
                        type: Array,
                        default: []
                    },
                }
            })

使用

<calendar :month="index+1" :month_calendar_day_rows="monthCalendarDayRows">
  	<template v-slot:header="scope">
	  <div class="calendar-header">{{scope.month}}</div>
	 </template>
	 <template v-slot:cell="{day}">
      	<div class="calendar-cell" :class="{'is-current-month':day.isCurrentMonth,'is-work-day':day.isWorkDay}">
           {{day.dayString}}
        </div>
    </template>
</calendar>

效果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值