vue 年日歷平鋪 12個月

image.png

1.安裝

// 安裝依賴
yarn add vue-material-year-calendary

2.創(chuàng)建組件

<template>
  <div class='yearCalendar'>
    <div class="yearBox">
      <el-button class="yearBtn" size="mini" @click="yearClick('clear')">清空選中日期</el-button>
      <span><i class="el-icon-d-arrow-left" @click="yearClick('reduce')"></i></span>
      <span class="yearText">{{currentYear}}年</span>
      <i class="el-icon-d-arrow-right" @click="yearClick('add')"></i>
      <el-button class="yearBtn" type="primary" size="mini" @click="yearClick('current')">切換到今年</el-button>
    </div>
    <YearCalendar
      v-model="currentYear"
      :activeDates="currentActiveDates"
      @toggleDate="toggleDate"
      prefixClass="your_customized_wrapper_class"
      :activeClass="activeClass"
      lang="tw"
      :showYearSelector="false"
    ></YearCalendar>
    <div class="btnBox">
      <el-button @click="yearCalendarClick(false)">取 消</el-button>
      <el-button type="primary" @click="yearCalendarClick(true)">確 定</el-button>
    </div>
  </div>
</template>

<script>
import YearCalendar from 'vue-material-year-calendar'
import { formatDay } from '@/utils/index.js'
export default {
  props: {
    activeDates: {
      type: Array,
      default() {
        return []
      }
    },
    year: {
      type: Number,
      default: new Date().getFullYear()
    }
  },
  components: {
    YearCalendar 
  },
  data:() => ({
    currentYear: new Date().getFullYear(),
    activeClass: '',
    currentActiveDates: []
  }),
  mounted() {
    // this.activeDates.push({
    //   date: formatDay(new Date()),
    //   className: 'blue'
    // })
  },
  created() {
    this.update()
  },
  watch: {
    activeDates: {
      handler(options) {
        this.update()
      },
      deep: true
    }
  },
  methods: {
    // 回顯
    update() {
      this.currentActiveDates = []
      if(this.activeDates.length) {
        this.activeDates.forEach(date => {
          this.currentActiveDates.push({
            date,
            className: 'blue'
          })
        })
      }
      this.currentYear = this.year ? this.year : new Date().getFullYear()
    },
    // 點擊日期
    toggleDate (dateInfo) {
      if(dateInfo.selected){
        this.currentActiveDates.push({
          date: dateInfo.date,
          className: 'blue'
        })
        // 只取兩組時間 當選擇時間多于2組時 清除第一個
        // this.currentActiveDates.length > 2 ?  this.currentActiveDates.shift() : ''
      }else {
        this.currentActiveDates.splice(this.currentActiveDates.findIndex(item => item.date == dateInfo.date), 1) // 再次點擊取消選中
      }
    },
    yearCalendarClick(flag) {
      if(flag){
        let dateList = []
        this.currentActiveDates.forEach(item => {
          dateList.push(item.date)
        })
        this.$emit('calendarChange', dateList)
      }else {
        this.$emit('calendarChange')
      }
    },
    // 切換年份
    yearClick(flag) {
      switch (flag) {
        case 'reduce': // 年份減少
          this.currentYear--
          
          break;
        case 'add': // 年份增加
          this.currentYear++
          this.currentActiveDates = []
          break;
        case 'current': // 實時年份
          if(this.currentYear != new Date().getFullYear()) {
            this.currentYear = new Date().getFullYear()
            this.currentActiveDates = []
          }
          break;
        case 'clear': // 清空選中的日期
          this.currentActiveDates = []
          break;
        default:
          break;
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.yearCalendar {
  .yearBox {
    color: rgb(182,194,205);
    font-size: 16px;
    text-align: center;
    padding: 10px 0;
    .yearText {
      font-size: 14px;
      color: rgb(81,88,96);
      padding: 0 50px;
    }
    .yearBtn {
      margin: 0 20px;
    }
  }
  .btnBox {
    margin-top: 8px;
    text-align: center;
  }
}
::v-deep .vue-calendar__container {
  background-color: #fff;
  box-shadow: 0 0 0 0 rgb(0 0 0 / 10%);
  .calendar__title {
    justify-content: left;
    height: 32px;
    line-height: 32px;
    border-bottom: 0;
    margin-bottom: 0;
    font-size: 12px;
  }
  .day__weektitle {
    height: 36px;
    opacity: 1;
    background: rgba(0, 8, 25, 0.05);
    font-size: 12px !important;
  }
  .calendar__body {
    padding: 0;
    background: #ffffff;
  }
  .calendar__day {
    color: #595e6a;
    font-size: 12px;
    font-weight: 400;
  }
  .container__months {
    padding: 0;
    flex: auto;
  }
  .container__month {
    // flex: 24%;
    flex: 16.3%;
    padding: 0 12px;
    border: 1px solid rgba(210,217,228,0.5);
    border-bottom: none;
  }
  .container__month:not(:first-child,:nth-child(7)) {
    border-left: none;
  }
  .calendar:hover {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgb(0 0 0 / 10%);
  }
  .calendar {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgb(0 0 0 / 10%);
    min-height: auto;
    width: 100%;
    padding-left: 0px;
    margin-bottom: 8px;
    .day {
      font-size: 12px;
    }
    .day:hover {
      border-radius: 50%;
      background-color: rgb(209,236,255);
    }
  }
  .grey {
    color: #bfc1c5;
  }
  .blue {
    color: #fff;
    background-color: rgb(31,160,254);
    border-radius: 50%;
  }
}
</style>

3.使用

<YearCalendar @calendarChange="calendarChange" :activeDates="form.stopDates" :year="form.year*1"/>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末空凸,一起剝皮案震驚了整個濱河市泥技,隨后出現(xiàn)的幾起案子明肮,更是在濱河造成了極大的恐慌缕棵,老刑警劉巖免猾,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異悟狱,居然都是意外死亡玄糟,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門即横,熙熙樓的掌柜王于貴愁眉苦臉地迎上來噪生,“玉大人,你說我怎么就攤上這事东囚《逅裕” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵页藻,是天一觀的道長桨嫁。 經(jīng)常有香客問我,道長份帐,這世上最難降的妖魔是什么璃吧? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮弥鹦,結果婚禮上肚逸,老公的妹妹穿的比我還像新娘。我一直安慰自己彬坏,他們只是感情好朦促,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著栓始,像睡著了一般务冕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上幻赚,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天禀忆,我揣著相機與錄音,去河邊找鬼落恼。 笑死箩退,一個胖子當著我的面吹牛,可吹牛的內容都是我干的佳谦。 我是一名探鬼主播戴涝,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了啥刻?” 一聲冷哼從身側響起奸鸯,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎可帽,沒想到半個月后娄涩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡映跟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年蓄拣,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片申窘。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡弯蚜,死狀恐怖,靈堂內的尸體忽然破棺而出剃法,到底是詐尸還是另有隱情碎捺,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布贷洲,位于F島的核電站收厨,受9級特大地震影響,放射性物質發(fā)生泄漏优构。R本人自食惡果不足惜诵叁,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望钦椭。 院中可真熱鬧拧额,春花似錦、人聲如沸彪腔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽德挣。三九已至恭垦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間格嗅,已是汗流浹背番挺。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留屯掖,地道東北人玄柏。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像贴铜,于是被迫代替她去往敵國和親禁荸。 傳聞我的和親對象是個殘疾皇子右蒲,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容