vue自定義可選時(shí)間的日歷組件
本文實(shí)例為大家分享了vue自定義可選時(shí)間日歷組件的具體代碼,供大家參考,具體內(nèi)容如下
日歷功能:
1、過(guò)去時(shí)間不可選擇
2、可自定義不可選時(shí)間
3、本月默認(rèn)展示當(dāng)天,其他月展示第一天,若為不可選時(shí)間,往后順延
效果圖:

-------下面開(kāi)始表演-----------
**首先,畫出日歷頁(yè)面布局,參照win10系統(tǒng)日歷布局*6行7列,為何如此,請(qǐng)自行理解…*本人也是“偷窺”來(lái)的
beginDay是當(dāng)前月第一天的周幾,prevMdays是上個(gè)月總天數(shù),nowMdays是當(dāng)月總天數(shù),這樣就實(shí)現(xiàn)了日歷的展示效果,標(biāo)簽中綁入一些可能會(huì)用到的數(shù)據(jù) data-dates等
<div class="dateContent-body-day" v-for="item in 42" :key="item">
<div
v-if="item - beginDay > 0 && item - beginDay <= nowMdays"
:class="{
'active-day': `${year}/${month}/${item - beginDay}` == curDate
}"
@click="dayHandler"
:data-year="year"
:data-month="month"
:data-day="item - beginDay"
:data-dates="year + '/' + month + '/' + (item - beginDay)"
>
{{ item - beginDay }}
</div>
<div class="other-day" v-else-if="item - beginDay <= 0">
{{ item - beginDay + prevMdays }}
</div>
<div class="other-day" v-else>{{ item - beginDay - nowMdays }}</div>
</div>
—接下來(lái)…
所用到的數(shù)據(jù):

*active-day是高亮的那一天即選中日期,curDate控制選中哪一天,這里默認(rèn)當(dāng)天,開(kāi)放一個(gè)props數(shù)據(jù)timeArry,允許傳入一些自定義日期作為不可選,點(diǎn)擊的日期中綁定的dates存在于數(shù)組中則返回,可選擇的情況下通過(guò)$emit將選擇的結(jié)果通過(guò)chooseDate事件暴露出去。
//點(diǎn)擊切換日
dayHandler(e) {
console.log(e);
var daNum = e.target.dataset;
if (this.cantTime.indexOf(daNum.dates) > -1) {
this.$toast("非開(kāi)放日期,不可選取");
return;
}
if (
`${daNum.year}/${daNum.month}/${daNum.day}` >=
`${new Date().getFullYear()}/${new Date().getMonth() +
1}/${new Date().getDate()}`
) {
this.date = e.target.dataset.day;
this.$emit(
"chooseDate",
this.year + "/" + this.month + "/" + this.date
);
} else {
this.$toast("過(guò)去時(shí)間不可選取");
}
},
//切換下月
``nextMonth() {
if (this.month == 12) {
this.month = 1;
this.year++;
} else {
this.month++;
}
},
對(duì)切換月、日都要進(jìn)行觀測(cè),重點(diǎn)在于觀測(cè)月份變化,也不知道watch有沒(méi)有被濫用
```javascript
watch: {
date(val, oldval) {
if (val) {
this.curDate = `${this.year}/${this.month}/${this.date}`;
}
},
month(val, oldval) {
if (val) {
var ndate;
for (var i = 1; i <= 31; i++) {
console.log(`${this.year}/${this.month}/${i}`);
if (this.cantTime.indexOf(`${this.year}/${this.month}/${i}`) < 0) {
console.log("不存在數(shù)值,停止,日期停留在" + i);
ndate = i;
break;
}
}
console.log(ndate, `${this.year}/${this.month}/${ndate}`);
//用切換到的月和本日相比較,未來(lái)月默認(rèn)選中1號(hào),當(dāng)月選中當(dāng)天
if (
`${this.year}/${this.month}/1` >
`${new Date().getFullYear()}/${new Date().getMonth() +
1}/${new Date().getDate()}`
) {
this.curDate = `${this.year}/${this.month}/${ndate}`;
this.date = ndate;
} else {
for (var i = new Date().getDate(); i <= 31; i++) {
console.log(2`${this.year}/${this.month}/${i}`);
if (this.cantTime.indexOf(`${this.year}/${this.month}/${i}`) < 0) {
this.curDate = `${new Date().getFullYear()}/${new Date().getMonth() +
1}/${i}`;
this.date = i;
break;
}
}
}
this.$emit(
"chooseDate",
this.year + "/" + this.month + "/" + this.date
);
}
}
},
父組件中調(diào)用
<calendar :timeArry="timeArray" @chooseDate="chooseHandler"></calendar>
import { calendar ,alertBox} from '@/components/index.js';
export default {
components:{calendar,alertBox
},
這樣的日歷就完成了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題
這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue中的.capture和.self區(qū)分及初步理解
這篇文章主要介紹了vue中的.capture和.self區(qū)分及初步理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue實(shí)現(xiàn)用戶自定義字段顯示數(shù)據(jù)的方法
今天小編就為大家分享一篇Vue實(shí)現(xiàn)用戶自定義字段顯示數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
淺談Vuex的this.$store.commit和在Vue項(xiàng)目中引用公共方法
這篇文章主要介紹了淺談Vuex的this.$store.commit和在Vue項(xiàng)目中引用公共方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue 項(xiàng)目打包時(shí)樣式及背景圖片路徑找不到的解決方式
今天小編就為大家分享一篇vue 項(xiàng)目打包時(shí)樣式及背景圖片路徑找不到的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
解讀vue項(xiàng)目中遇到的深拷貝淺拷貝問(wèn)題
這篇文章主要介紹了vue項(xiàng)目中遇到的深拷貝淺拷貝問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vite?Vue3?EsLint?Prettier配置步驟極簡(jiǎn)方法詳解
這篇文章主要為大家介紹了Vite?Vue3?EsLint?Prettier配置步驟的極簡(jiǎn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Vue?keepAlive實(shí)現(xiàn)不同的路由共用一個(gè)組件component的緩存問(wèn)題(推薦)
這篇文章主要介紹了Vue?keepAlive實(shí)現(xiàn)不同的路由共用一個(gè)組件component的緩存問(wèn)題,實(shí)現(xiàn)方式使用Vue?keepAlive實(shí)現(xiàn)頁(yè)面緩存,需要的朋友可以參考下2022-08-08

