vue 簡單日曆組件

首頁

<!--showToday:是否顯示返回今天按鈕 on-click:監聽組件內的點擊事件-->
<template>
    <my-calendar :showToday='true' @on-click='showInfo'></my-calendar>
</template>

<script>
import myCalendar from './components/myCalendar'
export default {
  components: {
    myCalendar
  },
  methods: {
    showInfo(data){
        if(data == 0){
            alert('你點擊了上一個月')
        }else{
            alert('你點擊了下一個月')
        }
    }
  }
}
</script>

組件

<template>
    <div class="cal-wrap">
        <div class="cal-top">
            <Affix :offset-top="0">
                <div class="cal-YM">
                    <Spin v-if='calLoading' fix>
                        <Icon type="load-c" size=18 class="demo-spin-icon-load"></Icon>
                    </Spin>
                    <div class="YM-text ovh">
                        <div title='上一月' class="cal-left hand fl" @click="getPrevMonth"><Icon type="ios-arrow-left"></Icon></div>
                        {{calendar.year}}年-{{calendar.month}}月<span @click="backToday" class='hand' title="返回今天" v-if='showToday'>今</span>
                        <div title='下一月' class="cal-right hand fr"  @click="getNextMonth"><Icon type="ios-arrow-right"></Icon></div>
                    </div>
                </div>
                <div class="cal-week-wrap ovh">
                    <div class="cal-week red">日</div>
                    <div class="cal-week" v-for="(item,index) in calendar.weeks" :key="index">{{item}}</div>
                    <div class="cal-week red">六</div>
                </div>
            </Affix>
        </div>

        <table class="cal-table mb20">
            <tr v-for="(item,itemIndex) in calendar.dayList" :key='itemIndex'>
                <td v-for="(key,keyIndex) in item" :key='key.date' :class="{'bg-grey':key.disable}">
                    <div class="cal-item" :class="{'cal-active':calendar.isDay == key.date}">
                        <span>{{key.day}}</span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</template>

<script>
export default{
    mounted(){
        this.backToday();
    },
    data(){
        return {
            calLoading:false,
            calendar:{//日曆
                dayList:[],//二維數組,循環行,循環列
                prev:[],
                current:[],
                next:[],
                year:'',
                month:'',
                weeks:['一','二','三','四','五'],
                isDay:''//判斷是否是'今天'
            }
        }
    },
    props:{
        showToday:{
            type:Boolean,
            default:false//是否顯示返回今天按鈕
        }
    },
    methods:{
        initDate(val){
            if(val < 10){
                return '0'+val;
            }else{
                return val;
            }
        },
        getLastDate(year,month){
            return new Date(year,month,0);
        },
        getmonthDays(){//獲取上月 當前月和下月天數
            let that=this;
            let y=that.calendar.year;
            let m=that.calendar.month;
            let preYear;//上一年
            let preMonth;//上一月
            let nextYear;//下一年
            let nextMonth;//下一月
            that.calendar.current=[];
            that.calendar.prev=[];
            that.calendar.next=[];

            // 當前月天數
            for(let i=1; i<=that.getLastDate(y,m).getDate(); i++){
                //date用於日期判斷,day用於顯示,flag用於狀態判斷
                that.calendar.current.push({date:y+'-'+m+'-'+that.initDate(i),day:i,timeList:[],disable:true});
            }

            /*上月*/
            let d=that.getLastDate(y,m - 1).getDate();//上月一共多少天
            preYear= m == 1 ?  y-1 : y;//當前月是1月,那麼上一月的年份要-1
            preMonth= m == 1 ?  12 : that.initDate(parseInt(m)-1);//當前月是1月,那麼上一月是12月
            for(let j=(that.getLastDate(y,m - 1).getDay()); j >= 0; j--){
                that.calendar.prev.push({date:preYear+'-'+preMonth+'-'+(d-j),day:d-j,timeList:[],disable:true});
            }

            /*下月*/
            nextYear= m == 12 ?  y+1 : y;//當前月是12月,那麼下一月的年份要+1
            nextMonth= m == 12 ? '01' : that.initDate(parseInt(m)+1);//當前月是12月,那麼下一月是1月
            for(let k=1; k <= 42- that.calendar.current.length - that.calendar.prev.length; k++){
                that.calendar.next.push({date:nextYear+'-'+nextMonth+'-'+that.initDate(k),day:k,timeList:[],disable:true});
            }
            that.calendar.dayList=[];

            // 數組合並
            let tempArr=that.calendar.prev.concat(that.calendar.current,that.calendar.next);
            // 數組分組,每7個一組
            for(let i = 0;i < tempArr.length; i+=7){
                that.calendar.dayList.push(tempArr.slice(i, i+7));
            }
        },
        getPrevMonth(){//上一月
            if(this.calendar.month != 1){
                this.calendar.month = this.initDate(--this.calendar.month);
            }else{
                this.calendar.month = 12;
                this.calendar.year = --this.calendar.year;
            }   
            this.getmonthDays();
            this.currentDay();
            this.$emit('on-click',0);
        },
        getNextMonth(){//下一月
            if(this.calendar.month < 12){
                this.calendar.month = this.initDate(++this.calendar.month);
            }else{
                this.calendar.month = '01';
                this.calendar.year = ++this.calendar.year;
            } 
            this.getmonthDays();
            this.currentDay();
            this.$emit('on-click',1);
        },
        currentDay(){//獲取今天,高亮顯示今天
            let that=this;
            let date=new Date();
            let y=that.calendar.year;
            let m =that.calendar.month;

            if(y === date.getFullYear() && m == date.getMonth()+1){//如果是當年當月
                that.calendar.isDay = y+'-'+m+'-'+that.initDate(date.getDate());//獲取到今天的號數
            }else{
                that.calendar.isDay=-1;
            }
        },
        backToday(){//返回今天
            let that=this;
            let d=new Date();
            that.calendar.year=d.getFullYear();
            that.calendar.month=that.initDate(d.getMonth()+1);
            that.currentDay();
            that.getmonthDays();
        }
    }
}
</script>
<style>
.ivu-affix{background: #fff;}
.fl{float: left;}
.fr{float: right;}
.hand{cursor: pointer;}
.cal-wrap,.perconsult .cert-title + p{font-family:'SimSun';}
.cal-YM{text-align:center;font-size:28px;line-height:50px; width: 260px; margin:0 auto;}
.cal-body{ border-right:1px solid #eee; border-left:1px solid #eee;}
.cal-week-wrap{border:1px solid #eeeeee; border-bottom:none;}
.cal-week{width:14.28%;text-align:center;line-height:40px; font-size: 16px; float: left;}
.YM-text{ font-size: 18px;}
.YM-text span{display:inline-block;margin:-10px 0 0 10px;width:26px;height:26px;border-radius:3px;background:#f17437;line-height:26px;color:#fff; font-size: 14px;}
.cal-left,.cal-right{font-size: 22px; width: 20px; text-align: center;}
.cal-left:hover > i,.cal-right:hover > i{color:#3583f7};
.cal-table{width: 100%; border: 1px solid #eee; text-align: center; border-collapse: collapse;}
.cal-table tr{ border-bottom: 1px solid #eee;}
.cal-table tr td:first-child .cal-item > span,.cal-table tr td:last-child .cal-item > span{color:red;}
.cal-table td{border-right: 1px rgb(239, 239, 239) solid; cursor: pointer; vertical-align: top;}
.cal-table td:hover{background: #f5f5ed;}

.cal-item{width: 129px;padding:5px;min-height: 80px;}
.cal-item > span{display:block; width: 32px; height: 32px; border-radius: 50%; margin: 4px auto; line-height: 32px; font-size: 16px;}
.cal-active > span{ color:#fff!important; background: #3583f7;}
.cal-time-list{font-size: 12px; text-align: left;}
.cal-time-list > p{ width:100px;margin:0 auto; padding-left: 8px; height: 18px; line-height:18px;}
.cal-time-list > p >span{display:block; overflow: hidden;}
</style>

圖片描述

圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章