vue-week-picker實(shí)現(xiàn)支持按周切換的日歷
本文實(shí)例為大家分享了vue-week-picker實(shí)現(xiàn)按周切換的日歷的具體代碼,供大家參考,具體內(nèi)容如下
vue-week-picker
安裝
npm install vue-week-picker --save-dev
DEMO
功能
- 自適應(yīng)式按周切換
- 與DatePicker日期選擇器使用
結(jié)合Element-ui使用
效果
與vue-element結(jié)合組件,請(qǐng)轉(zhuǎn)到鏈接
使用
<VueWeekPicker @dateValue="dateValue" /> Or <vue-week-picker @dateValue="dateValue" /> import VueWeekPicker from 'vue-week-picker'; export default { components: { VueWeekPicker } } Or export default { components: { 'vue-week-picker': VueWeekPicker } }
代碼
<template> <div class="date"> <el-row> <el-col :span="24"> <div class="weeks"> <!-- 日期 --> <ul class="days"> <li @click="weekPre" class="prev-btn"> <i class="fa fa-angle-left fa-icon" aria-hidden="true"></i> <span class="hidden-sm-and-down" style="margin-left: 5px;">上一周</span> </li> <li @click="pick(day, index)" v-for="(day, index) in days" :key="index" :class="{selected:index == tabIndex}" > <!--本月--> <span v-if="day.getMonth()+1 != currentMonth" class="other-month item-wrapper"> <p>{{day | getWeekFormat}}</p> <span class="hidden-sm-and-down">{{ day | dateFormat }}</span> </span> <span v-else> <!--今天--> <span v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()" class="today-item" >今天</span> <span class="item-wrapper" v-else> <p>{{day | getWeekFormat}}</p> <span class="hidden-sm-and-down">{{ day | dateFormat }}</span> </span> </span> </li> <li @click="weekNext" class="next-btn"> <span class="hidden-sm-and-down" style="margin-right: 5px;">下一周</span> <i class="fa fa-angle-right fa-icon" aria-hidden="true"></i> </li> <li> <span> <el-date-picker class="right-pick-btn" style="width: 100%" @change="pickDate" v-model="value1" type="date" placeholder="按日期查詢" ></el-date-picker> </span> </li> </ul> </div> </el-col> </el-row> <el-row> <el-col :span="20" :offset="2" class="time-range"> <span @click="pickTime(time, index)" v-for="(time, index) in times" :key="index" :class="{active:index == tabTimeIndex}" >{{time}}</span> </el-col> </el-row> </div> </template>
<script> /* eslint-disable */ import moment from "moment"; export default { props: { dateValue: { type: String, default: moment(new Date()).format("YYYY-MM-DD") }, timeValue: { type: String, default: "00:00" } }, data() { return { currentYear: 1970, // 年份 currentMonth: 1, // 月份 currentDay: 1, // 日期 currentWeek: 1, // 星期 days: [], value1: "", tabIndex: null, tabTimeIndex: 0, times: [ "00:00~06:00", "06:00~12:00", "12:00~18:00", "18:00~24:00", "今日節(jié)目" ] }; }, filters: { dateFormat(date) { return moment(date).format("YYYY-MM-DD"); }, getWeekFormat(date) { const weeksObj = { 1: "周一", 2: "周二", 3: "周三", 4: "周四", 5: "周五", 6: "周六", 7: "周日" }; let weekNumber = moment(date).isoWeekday(); return weeksObj[weekNumber]; } }, mounted() { const index = _.findIndex(this.days, function(o) { // console.log('o: ', o.getDate()); // console.log('new Date().getDate(): ', new Date().getDate()); return o.getDate() === new Date().getDate(); }); console.log("index: ", index); this.tabIndex = index; }, created() { this.initData(null); }, methods: { formatDate(year, month, day) { const y = year; let m = month; if (m < 10) m = `0${m}`; let d = day; if (d < 10) d = `0$vvxyksv9kd`; return `${y}-${m}-$vvxyksv9kd`; }, pickDate(date) { let newDate = moment(date).format("YYYY-MM-DD"); this.$emit("dateValue", newDate); }, initData(cur) { let date = ""; if (cur) { date = new Date(cur); } else { date = new Date(); } this.currentDay = date.getDate(); // 今日日期 幾號(hào) this.currentYear = date.getFullYear(); // 當(dāng)前年份 this.currentMonth = date.getMonth() + 1; // 當(dāng)前月份 this.currentWeek = date.getDay(); // 1...6,0 // 星期幾 if (this.currentWeek === 0) { this.currentWeek = 7; } const str = this.formatDate( this.currentYear, this.currentMonth, this.currentDay ); // 今日日期 年-月-日 this.days.length = 0; // 今天是周日,放在第一行第7個(gè)位置,前面6個(gè) 這里默認(rèn)顯示一周,如果需要顯示一個(gè)月,則第二個(gè)循環(huán)為 i<= 35- this.currentWeek /* eslint-disabled */ for (let i = this.currentWeek - 1; i >= 0; i -= 1) { const d = new Date(str); d.setDate(d.getDate() - i); // console.log(y:" + d.getDate()) this.days.push(d); } for (let i = 1; i <= 7 - this.currentWeek; i += 1) { const d = new Date(str); d.setDate(d.getDate() + i); this.days.push(d); } }, // 上個(gè)星期 weekPre() { const d = this.days[0]; // 如果當(dāng)期日期是7號(hào)或者小于7號(hào) d.setDate(d.getDate() - 7); this.initData(d); }, // 下個(gè)星期 weekNext() { const d = this.days[6]; // 如果當(dāng)期日期是7號(hào)或者小于7號(hào) d.setDate(d.getDate() + 7); this.initData(d); }, // 上一個(gè)月 傳入當(dāng)前年份和月份 pickPre(year, month) { const d = new Date(this.formatDate(year, month, 1)); d.setDate(0); this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1)); }, // 下一個(gè)月 傳入當(dāng)前年份和月份 pickNext(year, month) { const d = new Date(this.formatDate(year, month, 1)); d.setDate(35); this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1)); }, // 當(dāng)前選擇日期 pick(date, index) { let newDate = moment(date).format("YYYY-MM-DD"); this.$emit("dateValue", newDate); // console.log("index: ", index); this.tabIndex = index; // alert( // this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate()) // ); }, pickTime(time, index) { // console.log('time: ', time); let timeArr = []; timeArr.push(_.head(_.split(time, "~"))); console.log("timeArr: ", timeArr); this.$emit("timeValue", _.join(timeArr), ""); // console.log("index: ", index); this.tabTimeIndex = index; // alert( // this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate()) // ); } } }; </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3使用echarts tree高度自適應(yīng)支持滾動(dòng)查看功能
這篇文章主要介紹了Vue3使用echarts tree高度自適應(yīng)支持滾動(dòng)查看功能,文章同通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-06-06Vant picker選擇器設(shè)置默認(rèn)值導(dǎo)致選擇器失效的解決
這篇文章主要介紹了Vant picker選擇器設(shè)置默認(rèn)值導(dǎo)致選擇器失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Vue如何動(dòng)態(tài)給id設(shè)置style樣式
這篇文章主要介紹了Vue如何動(dòng)態(tài)給id設(shè)置style樣式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06element?table?數(shù)據(jù)量大頁(yè)面卡頓的解決
這篇文章主要介紹了element?table?數(shù)據(jù)量大頁(yè)面卡頓的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01vue2老項(xiàng)目中node-sass更換dart-sass的操作方法
這篇文章主要介紹了vue2老項(xiàng)目中node-sass更換dart-sass的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-07-07vue watch關(guān)于對(duì)象內(nèi)的屬性監(jiān)聽(tīng)
這篇文章主要介紹了vue watch關(guān)于對(duì)象內(nèi)的屬性監(jiān)聽(tīng)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04