先看效果图:
1、日期的修改以及日期下面小圆点的添加:
elementui支持使用slot对日期中的参数进行修改。通过设置名为 dateCell 的 scoped-slot 来自定义日历单元格中显示的内容。在 scoped-slot 可以获取到 date(当前单元格的日期), data(包括 type,isSelected,day 属性)。
<div class="left-wrap">
<div class="calendar-wrap">
<el-calendar :first-day-of-week=7>
<template
slot="dateCell"
slot-scope="{date, data}">
<p>
{{ data.day.split('-').slice(2).join('-') }}<br />
</p>
<!--标记-->
<div v-if="data.day=='2020-08-23'||data.day=='2020-08-19'" class="red budge"></div>
<div v-if="data.day=='2020-08-09'||data.day=='2020-08-13'" class="green budge"></div>
<div v-if="data.day=='2020-08-12'||data.day=='2020-08-22'" class="orange budge"></div>
</template>
</el-calendar>
</div>
</div>
CSS代码:
<style>
.left-wrap /deep/ .el-calendar-table .el-calendar-day{
padding: 22px;
}
.left-wrap /deep/ .el-backtop, .el-calendar-table td.is-today p{
height: 30px;
width: 30px;
color: white;
border-radius: 15px;
line-height: 30px;
margin: 0 auto;
margin-top: -6px;
background-image: linear-gradient(to right, #2160dc, #4880f0);
}
.budge{
width: 10px;
height: 10px;
border-radius: 5px;
margin: 5px auto;
}
.red{
background-color: #c9413f;
}
.green{
background-color: #25b591;
}
.orange{
background-color: #ee915c;
}
</style>
2、每周开始第一天设置为周日
在elementui中,每周开始第一天默认为周一,在官网中提供修改方法:
<el-calendar :first-day-of-week=7>
3、给周末设置不同颜色的背景
一开始被这个地方卡住了,后来去看了一下日历的html源码结构:
每一行日历单元格都是包裹在<tr class="el-calendar-table__row">
中的,而周日、周六正好是该节点的第一个、最后一个孩子节点,于是我灵机一动:
.calendar-wrap /deep/ .el-calendar-table tr td:first-child,
.calendar-wrap /deep/ .el-calendar-table tr td:last-child{
background-color: #f9f8fe;
}
这样就OK了
4、上个月、下个月按钮
这是elementui的默认样式,按钮在日历的左上方,
先把默认的按钮隐藏掉:
.left-wrap /deep/ .el-calendar__button-group{
display: none;
}
添加自定义的按钮:
<el-button class="but prev"><i class="el-icon-arrow-left"></i></el-button>
<el-button class="but next"><i class="el-icon-arrow-right"></i></el-button>
设置CSS样式:
.calendar-wrap /deep/ .el-button{
height: 30px;
width: 30px;
border-radius: 15px;
padding: 0;
background-color: #aec7fc;
color: white;
}
.calendar-wrap /deep/ .el-icon-arrow-left,
.calendar-wrap /deep/ .el-icon-arrow-right{
font-weight:900 !important;
}
5、给自定义按钮添加绑定事件:
由于没能在源码中找到对应的方法,我想到了另一个办法,就是通过点击一个按钮触发另一个按钮的点击事件,即模拟点击事件。网上有方法说可以通过给另一个按钮添加ref="uploadImgBtn"
然后使用this.$refs.uploadImgBtn[0].$el.click()
触发事件。但是按钮默认隐藏在日历组件中,我没找到给按钮添加ref的方法。最后我使用了jquery:
$(".prev").click(function(){
$(".el-button-group>.el-button:first-child").trigger('click');
}),
$(".next").click(function(){
$(".el-button-group>.el-button:last-child").trigger('click');
})
大功告成