element?時(shí)間選擇器禁用選擇的使用示例
本文主要介紹了element 時(shí)間選擇器禁用選擇的使用示例,具體如下:
方法一、年份或月份選擇都默認(rèn)yyyy-MM(disabledDate中,time.getTime() 的范圍是禁用的時(shí)間范圍)
1、在forbiddenDate之后的日期禁用不可選擇
<el-date-picker v-model="financialYearMonth" :value-format="'yyyy-MM'" type="month" :size="'small'" @change="getCwszSubj" placeholder="選擇日期" :picker-options="monthpickoption" > </el-date-picker> export default { data() { return { monthpickoption:{ // 設(shè)置禁用狀態(tài),參數(shù)為當(dāng)前日期,要求返回 Boolean disabledDate: (time) => { // 把默認(rèn)日期轉(zhuǎn)時(shí)間戳 let date = new Date(this.forbiddenDate).getTime(); // 當(dāng)前日期時(shí)間戳 > 默認(rèn)日期時(shí)間戳 則禁止默認(rèn)以后的日期,反之如果是小于禁用之前的日期 return time.getTime() > date; }, }, } } }
2、在開(kāi)始日期和結(jié)束日期內(nèi)可以選擇,其他不可選擇,例如:yearMonthStart(開(kāi)始日期)、yearMonthEnd(結(jié)束日期)
如果yearMonthStart:2016-01、yearMonthEnd:2023-02,那就只有2016-01至2023-02范圍內(nèi)的日期可以選擇
<el-date-picker v-model="financialYearMonth" type="monthrange" value-format="yyyy-MM" range-separator="至" start-placeholder="開(kāi)始月份" end-placeholder="結(jié)束月份" :picker-options="pickerOptions" > </el-date-picker> export default { data() { return { pickerOptions:{ // 設(shè)置禁用狀態(tài),參數(shù)為當(dāng)前日期,要求返回 Boolean disabledDate: (time) => { // 開(kāi)始日期 let dateStart = new Date(that.yearMonthStart).getTime(); // 結(jié)束日期 let dateEnd = new Date(that.yearMonthEnd).getTime(); // 在開(kāi)始日期和結(jié)束日期內(nèi)可以選擇,其他不可選擇 return time.getTime() < dateStart || time.getTime() > dateEnd; }, }, } } } export default { computed:{ pickerOptions(){ let that = this; return { disabledDate(time) { // 開(kāi)始日期 let dateStart = new Date(that.yearMonthDisableStart).getTime(); // 結(jié)束日期 let dateEnd = new Date(that.yearMonthDisableEnd).getTime(); // 在開(kāi)始日期和結(jié)束日期內(nèi)可以選擇,其他不可選擇 return time.getTime() < dateStart || time.getTime() > dateEnd } } } }, }
方法二、
<div class="selectItem" v-show="monthShow"> <el-date-picker v-model="trendData.date" :clearable="false" type="monthrange" :default-value="trendData.date" :picker-options="monthpickoption" value-format="yyyy-MM" range-separator="至" @change="onChangeDate" start-placeholder="開(kāi)始日期" end-placeholder="結(jié)束日期"> </el-date-picker> </div> export default { data() { return { trendData: { date: [] }, minDate: null, maxDate: null, monthpickoption:{ // 設(shè)置禁用狀態(tài),參數(shù)為當(dāng)前日期,要求返回 Boolean disabledDate: (time) => { if (this.minDate !== null && this.maxDate === null) { let minMonth = this.minDate.getMonth(), lastYear = new Date(this.minDate).setMonth(minMonth - 11), nextYear = new Date(this.minDate).setMonth(minMonth + 11); let minTime = this.minDate.getTime() let nextTime = new Date().setMonth(new Date().getMonth() + 11) let lastTime = new Date().setMonth(new Date().getMonth() - 11) if (minTime <= nextTime || minTime >= lastTime){ //開(kāi)始日期少于當(dāng)前月+12個(gè)月 并且大于當(dāng)前月-12個(gè)月,則表示只限制前面的范圍 return time.valueOf() > new Date().getTime() || time.valueOf() > nextYear.valueOf() || time.valueOf() < lastYear.valueOf() } else { // 只能選 minDate 前后一年的范圍 return time.valueOf() < lastYear.valueOf() || time.valueOf() > nextYear.valueOf(); } }else { let startMonth = this.trendData.date[0] let month = new Date(startMonth).getMonth() let lastMonth = new Date(startMonth).setMonth(month - 11) //如果有默認(rèn)值,只禁用開(kāi)始日期,因?yàn)橐呀?jīng)限定了禁用未來(lái)范圍,且默認(rèn)值已經(jīng)為12個(gè)月范圍 return this.monthPick(time) || time < lastMonth.valueOf(); } }, // 選中日期后會(huì)執(zhí)行的回調(diào),只有當(dāng) daterange 或 datetimerange 才生效 onPick: ({minDate, maxDate}) => { this.minDate = minDate; this.maxDate = maxDate; } } } }, methods: { monthPick(time){ // 獲取當(dāng)前的月份信息 const date = new Date() // 獲取當(dāng)前的時(shí)間基本信息,值是這樣的: Tue Jul 20 2021 14:59:43 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間) const year = date.getFullYear() // 獲取當(dāng)前年份,值是這樣的: 2021 let month = date.getMonth() + 1 // 獲取當(dāng)前月份,值是這樣的: 6 getMonth()方法返回值是0-11,也就是1月份到12月份,所以要加上1,才是當(dāng)前月份 if (month >= 1 && month <= 9) { // 如果是1月到9月就要在前面補(bǔ)上一個(gè)0 比如:02、07 月份這樣表示 month = '0' + month } const nowDate = year.toString() + month.toString() // 轉(zhuǎn)換成字符串拼接,最終得到年和月,比如此時(shí)的時(shí)間是2021年7月20號(hào),所以nowDate的值是:202107 // 獲取時(shí)間選擇器的月份信息 const timeyear = time.getFullYear() // 獲取時(shí)間選擇器的年份(有很多) let timemonth = time.getMonth() + 1 // 與上面同理 if (timemonth >= 1 && timemonth <= 9) { timemonth = '0' + timemonth } const elTimeData = timeyear.toString() + timemonth.toString() // 返回,時(shí)間選擇器的日期月份大于當(dāng)前日期的月份,又因?yàn)閐isabledDate函數(shù)是控制月份禁用不可選 // 所以,最終就是:時(shí)間選擇器的月份大于當(dāng)前的月份,就都禁用掉,所以就實(shí)現(xiàn)了最終效果: // 大于等于當(dāng)前月份都不可選 return elTimeData > nowDate // 這里雖然是字符串,但是弱類(lèi)型語(yǔ)言js會(huì)做一個(gè)轉(zhuǎn)換,是可以比較大小的如: '202107' > '202008' }, } }
如果有些邏輯在data里處理不了 可以在methods處理:
8.64e7 是科學(xué)計(jì)數(shù)法算出的值,實(shí)際為(8.64×10×10×10×10×10×10×10),也就是一天毫秒數(shù)。
<el-date-picker v-model="yearMonth" type="date" placeholder="選擇日期" :picker-options="pickerOptions"> </el-date-picker> export default { data() { return { pickerOptions: { disabledDate: this.disabledGetTime } } }, methods:{ disabledGetTime(time) { return time.getTime() < Date.now() - 8.64e7 || time.getTime() > new Date(this.uptimed).getTime() - 8.64e7 }, } }
禁止周六周日
disabledDate(time) { return ( [0,6].includes(time.getDay()) ); },
到此這篇關(guān)于element 時(shí)間選擇器禁用選擇的使用示例的文章就介紹到這了,更多相關(guān)element 時(shí)間選擇器禁用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue獲取路由詳細(xì)內(nèi)容信息方法實(shí)例
獲取路由詳細(xì)內(nèi)容信息是我們?nèi)粘i_(kāi)發(fā)中經(jīng)常會(huì)遇到的需求,下面這篇文章主要給大家介紹了關(guān)于vue獲取路由詳細(xì)內(nèi)容信息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12淺談axios中取消請(qǐng)求及阻止重復(fù)請(qǐng)求的方法
在實(shí)際項(xiàng)目中,我們可能需要對(duì)請(qǐng)求進(jìn)行“防抖”處理。本文主要實(shí)現(xiàn)axios中取消請(qǐng)求及阻止重復(fù)請(qǐng)求,具有一定的參考價(jià)值,感興趣的可以了解一下2021-08-08Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能
這篇文章主要為大家詳細(xì)介紹了Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09前端Vue全屏screenfull通用解決方案及原理詳細(xì)分析
這篇文章主要給大家介紹了關(guān)于前端Vue全屏screenfull通用解決方案及原理的相關(guān)資料,使用screenfull這一第三方庫(kù)可以實(shí)現(xiàn)更穩(wěn)定的全屏功能,需要的朋友可以參考下2024-10-10vue3中各種類(lèi)型文件進(jìn)行預(yù)覽功能實(shí)例
在vue移動(dòng)端項(xiàng)目中經(jīng)常遇到這樣的需求,對(duì)一些上傳的附件可以點(diǎn)擊之后在線(xiàn)預(yù)覽,所以下面這篇文章主要給大家介紹了關(guān)于vue3中各種類(lèi)型文件進(jìn)行預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2021-09-09vue+elementUI 復(fù)雜表單的驗(yàn)證、數(shù)據(jù)提交方案問(wèn)題
這篇文章主要介紹了vue+elementUI 復(fù)雜表單的驗(yàn)證、數(shù)據(jù)提交方案,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Vuex 使用及簡(jiǎn)單實(shí)例(計(jì)數(shù)器)
這篇文章主要介紹了Vuex 使用及簡(jiǎn)單實(shí)例(計(jì)數(shù)器),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08關(guān)于Vue3過(guò)渡動(dòng)畫(huà)的踩坑記錄
在開(kāi)發(fā)中我們想要給一個(gè)組件的顯示和消失添加某種過(guò)渡動(dòng)畫(huà),可以很好的增加用戶(hù)體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于Vue3過(guò)渡動(dòng)畫(huà)踩坑的相關(guān)資料,需要的朋友可以參考下2021-12-12