el-calendar 考勤日历制作

效果图

日历.png

注意点:

1.element的日历组件没有方法调用, 要通过切换月份调接口, 可以通过 watch监听日历的value
2.给日历的日期添加如图样式, 可以参照遮罩层的代码(这方法写的比较烂, 大概就是仅仅能用的程度)

<template>
  <div class="calendar">
    <el-calendar v-model="value">
      <template slot="dateCell" slot-scope="{ data }">
        <div class="poR">
          <span>{{ data.day.split("-").slice(2).join("-") }}</span>
          <!-- 遮罩层 -->
          <div v-for="(item, index) in calendarArr" :key="index">
            <div v-if="(item.isDate.split('-').slice(0)[0]).indexOf(data.day.split('-').slice(0)[0]) != -1">
              <div v-if="(item.isDate.split('-').slice(1)[0]).indexOf(data.day.split('-').slice(1)[0])!=-1">
                <div v-if="(item.isDate.split('-').slice(2).join('-')).indexOf(data.day.split('-').slice(2).join('-'))!=-1">
                  <div 
                    :class="data.day === item.isDate && item.status === 1 ? 'isStautus1' : data.day === item.isDate && item.status === 2 ? 'isStautus2' : data.day === item.isDate && item.status === 3 ? 'isStautus3' : 'unActive'"
                     class="calendar-day"
                   >
                  </div>
                </div>
                <div v-else></div>
              </div>
              <div v-else></div>
            </div>
            <div v-else></div>
          </div>
        </div>
      </template>
    </el-calendar>
  </div>
  <div class="stats">
    <div>{{ queryMonth .split('-').slice(0)[0] }}年{{ queryMonth.split('-').slice(1).join(['-']) }}月出勤统计</div>
      <ul class="flex">
        <li>
          <p>出勤</p>
          <p class="Cgreen">{{ calendarData.status1 }}</p>
        </li>
        <li>
          <p>加班</p>
          <p class="Cblue">{{ calendarData.status2 }}</p>
        </li>
        <li>
          <p>迟到</p>
          <p class="Cred">{{ calendarData.status3 }}</p>
        </li>
        <li>
          <p>早退</p>
          <p class="Cred">{{ calendarData.status4 }}</p>
        </li>
        <li>
          <p>缺勤</p>
          <p class="Cred">{{ calendarData.status5 }}</p>
        </li>
      </ul>
    </div>
    <div class="status">
      <span>
        <i class="dot bgcGreen"></i>
        正常
      </span>
      <span>
        <i class="dot bgcBlue"></i>
        加班
      </span>
    <span>
      <i class="dot bgcRed"></i>
      异常
    </span>
  </div>
< /template>

<script>
  export default {
    data() {
      return {
        value: new Date(), // 当前月份
        queryMonth: this.$tool.dateToOb(new Date()).year + "-" + this.$tool.dateToOb(new Date()).month, // 查询月份 yyyy-MM
        dateObj: {
          isDate: "",
          status: "" // 正常 加班 异常
        },
        calendarArr: [],
        calendarData: {
          status1: "",
          status2: "",
          status3: "",
          status4: "",
          status5: ""
        }
      }
    },
    // 切换月份
    watch: {
      value: function() {
        this.dateObj.status = ""
        this.dateObj.isDate = ""
        this.calendarArr = []
        var year = this.value.getFullYear();
        var month = this.value.getMonth() + 1;
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        this.queryMonth = year + "-" + month;
        getAttenceRecords({ queryMonth: this.queryMonth }).then(res => {
          this.calendarData = res.data;
          res.data.items.forEach(el => {
            this.dateObj = {}
            this.dateObj.isDate = el.recordTime
            this.dateObj.status = el.status
            this.calendarArr.push(this.dateObj)
          })
        })
      }
    },
  }
</script>

<style lang="scss" scoped>
  // 日历样式
  .calendar {
    /deep/ .el-calendar__header {
      background-color: #fbfbfb;
      border: 1px solid #ebeef5;
      border-bottom: none;
    }
    /deep/ .el-calendar__body {
      padding: 0;
    }
    /deep/ .el-calendar-table thead th {
      border-top: 1px solid #ebeef5;
      border-right: 1px solid #ebeef5;
    }
    /deep/ .el-calendar-table thead th:first-child {
      border-left: 1px solid #ebeef5;
    }
    /deep/ .el-calendar-table .el-calendar-day {
      height: 40px;
      line-height: 40px;
      padding: 0;
      text-align: center;
    }
  }
  .calendar-day {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    opacity: 0.6;
  }
  .isStautus1 {
    background-color: #66ff7c;
  }
  .isStautus2 {
    background-color: #00a0e9;
  }
  .isStautus3 {
    background-color: #ff0000;
  }
  // 出勤统计
  .stats {
    > div {
      text-align: center;
      padding: 10px 0;
      border-left: 1px solid #ebeef5;
      border-right: 1px solid #ebeef5;
      background-color: #fbfbfb;
    }
    ul {
      border-top: 1px solid #ebeef5;
      border-left: 1px solid #ebeef5;
      li {
        flex: 1;
        padding: 20px 0;
        border-right: 1px solid #ebeef5;
        border-bottom: 1px solid #ebeef5;
        p {
          padding: 10px;
          text-align: center;
        }
      }
    }
  }
  .status {
    padding-top: 20px;
    span {
      margin-right: 30px;
    }
    .dot {
      display: inline-block;
      width: 8px;
      height: 8px;
      border-radius: 50%;
    }
  }
}
// 字体颜色
.Cgreen {
  color: #66ff7c;
}
.Cblue {
  color: #00a0e9;
}
.Cred {
  color: #ff0000;
}
// 背景颜色
.bgcGreen {
  background-color: #66ff7c;
}
.bgcBlue {
  background-color: #00a0e9;
}
.bgcRed {
  background-color: #ff0000;
}
</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文为2016年11月9日,『前端之巅』微信群在线分享活动总结整理而成,转载请在文章开头处注明来自『前端之巅』公众...
    尾尾阅读 10,706评论 3 32
  • 头有些不舒服,不舒服,不舒服。 凉凉的,凉凉的,凉凉的。 鼻子也塞塞的,堵堵的。 是它们,把我从睡梦中唤醒 在闹铃...
    窗前的小豆豆Y阅读 28评论 0 0
  • 头像设置 头像设置资料
    Harely阅读 172评论 0 0
  • 当被问到这个问题的时候,你可不会这样回答—— 《昨日的美食》 也可以叫夫夫的快乐晚餐生活。 笕史朗(西岛秀俊)作为...
    灯前雪阅读 489评论 0 0
  • 余生,一定要找一个愿意陪你说废话的人。因为,只有这样的人,才是真正爱你的。我们应该知道,只有真正爱你的人,才愿意陪...
    寻一个树洞阅读 942评论 0 0