zoukankan      html  css  js  c++  java
  • 好用的日历控件

    传入参数date如:new Date()

    <template>
        <div class="calendar">
            <div class="week-box">
                <span v-for="item in day" class="color-999999 font-14">{{item}}</span>
            </div>
            <div>
                <div v-for="item in calendarData" class="xy-calendar-module">
                    <span v-for="day in item" class="box color-666666">{{day.day}}</span>
                </div>
            </div>
        </div>
    </template>
    
    <script>
        export default {
            props: {
              date: {
                type: Date,
                default: new Date()
              }
            },
            data() {
                return {
                    day: ['日', '一', '二', '三', '四', '五', '六'],
                    calendarData: [],
                    nowYear: new Date().getFullYear(),
                    nowMonth: new Date().getMonth() + 1,
                    nowDay: new Date().getDate(),
                }
            },
            mounted () {
              if (!!this.$props.date) {
                this.setDate();
              }
              this.initData(this.nowYear, this.nowMonth);
            },
            watch: {
                date (val) {
                    this.setDate();
                    this.initData(this.nowYear, this.nowMonth);
                }
            },
            methods: {
              setDate () {
                this.nowYear = new Date(this.$props.date).getFullYear()
                this.nowMonth = new Date(this.$props.date).getMonth() + 1
                this.nowDay = new Date(this.$props.date).getDate()
              },
              initData (year,month) {
                const days = this.days(year,month);  // 当前月份天数
                let enableSelect;
                let arr = Array.from({length: days}, (x, index )=>index + 1).map(item => {
                    return {
                        day: item,
                        name: this.getDayName(year, month, item),
                        week: this.getDateWeek(year, month, item)
                    }
                });
            // 最前边是星期一
                let beforeDayNum = arr[0].week - 1;
                let afterDayNum = 7 - arr[days-1].week;
            // 最前边是星期日
                let beforeDayNum = arr[0].week == 7 ? 0 : arr[0].week;
                let afterDayNum = 6 - arr[days-1].week == -1 ? 6 : 6 - arr[days-1].week;
    
                var beforeDayArr = this.completionDays(beforeDayNum);
                var afterDayArr = this.completionDays(afterDayNum);
                var allArr = [...beforeDayArr, ...arr, ...afterDayArr];
                this.calendarData = this.splitArray(allArr);
                console.log(afterDayNum)
              },
              splitArray (arr) {
                var result = [];
                for(var i=0; i < arr.length; i+=7){
                    result.push(arr.slice(i,i+7));
                }
                return result
              },
              completionDays (num) {
                return Array.from({length: num}, (x, index )=>index + 1).map(item => {
                return {
                    day: null,
                    week: null
                }
                })
              },
              // 一个月多少天
              days(year,month){
                var dayCount;
                let now = new Date(year,month, 0);
                dayCount = now.getDate();
                return dayCount;
              },
              // 某一天星期几
              getDateWeek (year,month,day) {
                var  tmpdate  =  new Date(year,month-1,day).getDay();
                return tmpdate === 0 ? 7 : tmpdate;
              },
              getToday () {
                let nowYear =  new Date().getFullYear();
                let nowMonth = new Date().getMonth() + 1;
                let nowDay = new Date().getDate();
                return {
                    nowYear,
                    nowMonth,
                    nowDay
                }
              },
              getDayName (year, month, day) {
                    if (year == this.getToday().nowYear && month == this.getToday().nowMonth && day == this.getToday().nowDay) {
                    return '今天'
                }
                return `${day}`
              }
            }
        }
    </script>
    
    <style scoped>
        .calendar{
            margin: 0 auto;
            background: #FFFFFF;
        }
        .xy-calendar-module{
            position: relative;
            display: flex;
            height: .44rem;
            line-height: .44rem;
        }
        .box{
            flex: 1;
            position: relative;
            text-align: center;
        }
        .week-box{
            display: flex;
            height: .44rem;
            line-height: .44rem;
        }
        .week-box span{
            flex: 1;
            text-align: center;
        }
    </style>
  • 相关阅读:
    Oracle Instant Client(即时客户端) 安装与配置
    面试中经典的数据库问题
    MySQL 大表优化方案
    mysql中Timestamp,Time,Datetime 区别
    HTML学习之给div高度设置百分比不生效的问题
    textarea文本域宽度和高度width及height自动适应实现代码
    JAVA基础----java中E,T,?的区别?
    去除ArrayList集合中的重复自定义对象元素
    sql select中加入常量列
    mysql 将null转代为0
  • 原文地址:https://www.cnblogs.com/xjy20170907/p/12161770.html
Copyright © 2011-2022 走看看