element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的方法
element的datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周
實(shí)現(xiàn)效果
頁(yè)面初始化效果

點(diǎn)擊上一周

點(diǎn)擊下一周

實(shí)現(xiàn)選擇周一和周日過(guò)程
1、采用el-date-picker組件,類型使用 week
<el-date-picker
v-model="weeklyDate"
type="week"
@change="newDateWeekHandle"
placeholder="選擇周" style="width: 230px">
</el-date-picker>但是范圍是從周日開(kāi)始,從周一開(kāi)始需要將 firstDayOfWeek 設(shè)置為 1
<el-date-picker
v-model="weeklyDate"
type="week"
:picker-options="{'firstDayOfWeek': 1}"
@change="newDateWeekHandle"
placeholder="選擇周" style="width: 230px">
</el-date-picker>2、框里還需要展示周一和周日的范圍,設(shè)置兩個(gè)新的屬性startDate和endDate
<el-date-picker
:format="startDate + ' 至 ' + endDate"
v-model="weeklyDate"
type="week"
:picker-options="{'firstDayOfWeek': 1}"
@change="newDateWeekHandle"
placeholder="選擇周" style="width: 230px">
</el-date-picker>根據(jù)model屬性(默認(rèn)是星期二),來(lái)算出展示的星期一和周期日
newDateWeekHandle(){
const now = new Date(this.weeklyDate);
const nowTime = now.getTime();
const day = now.getDay();
const oneDayTime = 24*60*60*1000;
const mondayTime = nowTime - (day-1)*oneDayTime;
const sundayTime = nowTime + (7-day)*oneDayTime;
this.startDate = this.$moment(mondayTime).format('YYYY-MM-DD');
this.endDate = this.$moment(sundayTime).format('YYYY-MM-DD');
},這里用到了moment.js,需要引入
3、下載moment
npm install moment --save
為了可以全局使用到,在main.js中全局方法掛載
import moment from 'moment' Vue.prototype.$moment = moment
4、選擇之后才能展示范圍,需要在頁(yè)面初始化時(shí)就給model屬性賦值
- 在created中調(diào)用getDateWeek方法
- 同時(shí)進(jìn)行計(jì)算周一和周日
getDateWeek(){
const now = new Date();
const nowTime = now.getTime();
const day = now.getDay();
const oneDayTime = 24*60*60*1000;
const mondayTime = nowTime - (day-2)*oneDayTime;//默認(rèn)是周二
this.weeklyDate = new Date(mondayTime);
this.newDateWeekHandle();
},實(shí)現(xiàn)上一周和下一周的切換功能
實(shí)際上就是計(jì)算model屬性值就可以了,通過(guò)Date的setDate方法實(shí)現(xiàn)日期的相加減,然后根據(jù)新得到的日期計(jì)算就可以了
[參考文章][//m.fzitv.net/javascript/299660yzs.htm]
handleLast(){
const last = new Date(this.weeklyDate);
last.setDate(last.getDate()-7);//日期相加減
this.weeklyDate = last;
this.newDateWeekHandle();
},
handleNext(){
const next = new Date(this.weeklyDate);
next.setDate(next.getDate()+7);
this.weeklyDate = next;
this.newDateWeekHandle();
},總結(jié)
到此這篇關(guān)于element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的文章就介紹到這了,更多相關(guān)element datepicker日期選擇器選擇日期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用Props實(shí)現(xiàn)組件數(shù)據(jù)交互的示例代碼
在Vue中,組件的props屬性用于定義組件可以接收的外部數(shù)據(jù),這些數(shù)據(jù)來(lái)自父組件并傳遞給子組件,本文給大家介紹了Vue使用Props實(shí)現(xiàn)組件數(shù)據(jù)交互,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-06-06
vue.js移動(dòng)端app實(shí)戰(zhàn)1:初始配置詳解
這篇文章主要介紹了vue.js移動(dòng)端app實(shí)戰(zhàn)1:初始配置詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
詳解如何實(shí)現(xiàn)Element樹(shù)形控件Tree在懶加載模式下的動(dòng)態(tài)更新
這篇文章主要介紹了詳解如何實(shí)現(xiàn)Element樹(shù)形控件Tree在懶加載模式下的動(dòng)態(tài)更新,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04

