vue 平铺式日历--在原基础上增加小圆点功能

原插件效果:

需求效果:

由于原插件无小圆点功能,所以增加了一个小圆点数组属性,另外注意,由于是vue,修改插件会比较麻烦,所以本人把插件单独作为组件放到component里面引入,而非直接下载使用。

使用的插件:

vue-material-year-calendar(可自行在GitHub搜步骤)以下步骤,按需求更改过,红色框为自己添加内容)

使用步骤:

1.安装插件:

npm install vue-material-year-calendar --save
2.导入插件:

import YearCalendar from 'vue-material-year-calendar'
3.代码实现:

index.vue:(引入插件的页面)

图中代码:

.your_customized_wrapper_class{
  background-color: #0aa;color: white
}
.calendar .day{
  color: black;
}
.my-blue{
  color: blue!important;
}
.my-red{
  color: red!important;
}
.calendar .day.my-blue::after{
  content: '';top: unset!important;right: 11px!important;bottom: -5px!important;
  width: 5px!important;height: 5px!important;background: blue!important;
  transform: translateX(50%);
}
.calendar .day.my-red::after{
  content: '';top: unset!important;right: 11px!important;bottom: -5px!important;
  width: 5px!important;height: 5px!important;background: red!important;
  transform: translateX(50%);
}

MonthCalendar.vue :(插件的页面)

图中代码:

this.obviousDates.forEach(date => {
  let oDate = date
  let dayjsObj = dayjs(oDate.date)
  if (dayjsObj.year() !== this.year) return
  let obviousDate = dayjsObj.date()
  let row = Math.floor(obviousDate / 7)
  let obviousArrayKey = (obviousDate % 7) - 1 + firstDay + 7 * row
  this.showDays[obviousArrayKey].active = true // to array index
  this.showDays[obviousArrayKey].className = (this.showDays[obviousArrayKey].className || '') + ' ' + oDate.className
})

Yearcanlendaar.vue:(插件的页面)

图中代码:

obviousDates: {
  type: Array,
  default: () => [],
  validator: (dateArray) => {
    let isGood = true
    let curDate = null
    dateArray.forEach(date => {
      if (typeof date === 'string') {
        curDate = date
      } else if (typeof date === 'object' && date.hasOwnProperty('date')) {
        curDate = date.date
      }
      // 以下程式碼參考「How to validate date with format mm/dd/yyyy in JavaScript?」in Stackoverflow// 由於 「^\d{4}\-\d{1,2}\-\d{1,2}$」會被ESLint 判為錯誤,所以暫時關閉 EsLint 對下一行的驗證 by丁丁// eslint-disable-next-line no-useless-escape
      if (!/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(curDate)) {
        isGood = false
      }
      // Parse the date parts to integers
      var parts = curDate.split('-')
      var day = parseInt(parts[2], 10)
      var month = parseInt(parts[1], 10)
      var year = parseInt(parts[0], 10)
      if (year < 1000 || year > 3000 || month === 0 || month > 12) isGood = false
      let monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
      if (year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)) monthLength[1] = 29
      if (!(day > 0 && day <= monthLength[month - 1])) isGood = false
    })
    return isGood
  }
},

图中代码:

month1 () {
  const month = {}
  this.obviousDates.forEach(date => {
    let oDate

    if (typeof date === 'string') {
      oDate = {
        date: date,
        className: this.activeClass
      }
    } else {
      // 若 activeDate 裡的物件少了 className 屬性,就自動填入空字串。否則會變成undefined
      oDate = {
        date: date.date,
        className: date.className || ''
      }
    }
    if (dayjs(oDate.date).year() !== parseInt(this.value)) return // 讓2020年1月的資料,不會放到 2019年的1月資料裡
    let m = (dayjs(oDate.date).month() + 1).toString()
    if (!month[m]) month[m] = []
    month[m].push(oDate)
  })
  return month
},
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值