vue 日程日历组件应用

一般涉及后台系统 或者移动端 都比较普遍,习惯性使用 插件什么的 ,说实话 个人比较自写组件 ,不显得臃肿, 就阔以完成所需要的  应用快速  bug不在 工作量降低   

上传一段组件吧

<template>
  <div class="cal-wrapper">
    <div class="cal-header">
      <div class="l">考勤日历</div>
      <div class="r">
        <div class="iconfont icon-jiantouleft icon" @click="preMonth"></div>
        <div class="title icon">{{curYearMonth}}</div>
        <div class="iconfont icon-jiantouright icon" @click="nextMonth"></div>
      </div>
    </div>
    <div class="cal-body">
      <div class="weeks">
        <span v-for="(dayName, dayIndex) in dayNames" :key="dayIndex" :style="`width:${100/dayNames.length}%;`">
          {{dayName}}
        </span>
      </div>
      <div class="dates">
        <div v-for="(date,index) in dayList" :key="index" :style="bgColor(date)" :class="[{ today: date.status ? (today == date.date) : false,event:date.status ? date.type : false}]" @click="unusualFunc(date)">
          <p class="date-num" :style="`color:${date.type?'#fff':date.status?'#74797b':'#b0b3b4'};`">{{date.date.split('/')[2]}}</p>
          <span>{{date.desc}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  props: {
    calendar: {
      type: Object,
      required: true
    },
    events: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      emptyText,
      dayNames,
      showUnusual: false,
      tableData: [],
      worknData: [],
      currentDay: "",
      checkDate: ""
    };
  },
  methods: {
    nextMonth() {
      this.$emit("month-changed", "next");
    },
    preMonth() {
      this.$emit("month-changed", "pre");
    },
    bgColor(date) {
      // 1 迟到 #f68787 2 未打卡 #56bff5 3 早退 #edb55d
      let bg =
        date.type == 1
          ? "#f68787"
          : date.type == 2 ? "#56bff5" : date.type == 3 ? "#edb55d" : "#fff";
      return `width:${100 / 7}%;background:${bg};color:#fff;`;
    },
    
      }
    }
  },

  computed: {
    dayList() {
      let firstDay = new Date(
        this.calendar.params.curYear,
        this.calendar.params.curMonth,
        1
      );
      let dayOfWeek = firstDay.getDay();
      // 根据当前日期计算偏移量 // Calculate the offset based on the current date
      if (this.calendar.options.weekStartOn > dayOfWeek) {
        dayOfWeek = dayOfWeek - this.calendar.options.weekStartOn + 7;
      } else {
        dayOfWeek = dayOfWeek - this.calendar.options.weekStartOn;
      }

      let startDate = new Date(firstDay);
      startDate.setDate(firstDay.getDate() - dayOfWeek);

      let item,
        status,
        tempArr = [],
        tempItem;
      for (let i = 0; i < 42; i++) {
        item = new Date(startDate);
        item.setDate(startDate.getDate() + i);

        status = this.calendar.params.curMonth === item.getMonth() ? 1 : 0;

        tempItem = {
          date: `${item.getFullYear()}/${item.getMonth() +
            1}/${item.getDate()}`,
          status: status,
          customClass: []
        };
        this.events.map(event => {
          if (isEqualDateStr(event.date, tempItem.date)) {
            tempItem.type = event.type;
            tempItem.desc = event.desc || "";
            if (event.customClass) tempItem.customClass.push(event.customClass);
          }
        });
        tempArr.push(tempItem);
      }
      return tempArr;
    },
    curYearMonth() {
      return `${this.calendar.params.curYear}年${this.calendar.params.curMonth +
        1}月`;
    },
    today() {
      let dateObj = new Date();
      return `${dateObj.getFullYear()}/${dateObj.getMonth() +
        1}/${dateObj.getDate()}`;
    }
  }
};
</script>

//样式我就不卖弄了 按需求调试吧

 直接运用

<cal-panel :calendar="calendarOptions" :events="event" @month-changed="handleMonthChanged"></cal-panel>



let dateObj = new Date();


calendarOptions: {
        options: {
          weekStartOn: 0
        },
        params: {
          curYear: dateObj.getFullYear(),
          curMonth: dateObj.getMonth(),
          curDate: dateObj.getDate(),
          curEventsDate: "all"
        }
      },
      event: [
        {
          date: `2018/07/21`,
          type: 1,
          desc: "迟到"
        },
        {
          date: `2018/07/24`,
          type: 2,
          desc: "未打卡"
        },
        {
          date: `2018/07/05`,
          type: 3,
          desc: "早退"
        },
        {
          date: `2018-07-16`,
          type: 1,
          desc: "迟到"
        },
        {
          date: `2018/07/10`,
          type: 2,
          desc: "未打卡"
        }
      ]

代码我就上传到这了  

扩能会有点问题  还需要优化一波

来个效果图吧

有些涉及工作机密 但不透露了  

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
对于自定义月日历日程组件,你可以使用Vue.js框架来实现。下面是一个简单的示例代码,可以帮助你开始构建这个组件: ```vue <template> <div class="calendar"> <div class="header"> <button @click="previousMonth"><</button> <h2>{{ currentMonth }}</h2> <button @click="nextMonth">></button> </div> <div class="days"> <div v-for="day in days" :key="day" class="day">{{ day }}</div> </div> <div class="events"> <div v-for="event in events" :key="event.id" class="event"> <p>{{ event.date }}</p> <p>{{ event.title }}</p> </div> </div> </div> </template> <script> export default { data() { return { currentMonth: '', days: [], events: [ { id: 1, date: '2022-01-05', title: 'Event 1' }, { id: 2, date: '2022-01-15', title: 'Event 2' }, { id: 3, date: '2022-01-20', title: 'Event 3' }, ], }; }, mounted() { this.setCurrentMonth(); this.setDays(); }, methods: { setCurrentMonth() { const date = new Date(); const options = { month: 'long', year: 'numeric' }; this.currentMonth = date.toLocaleDateString('en-US', options); }, setDays() { const date = new Date(); const year = date.getFullYear(); const month = date.getMonth(); const daysInMonth = new Date(year, month + 1, 0).getDate(); this.days = Array.from({ length: daysInMonth }, (_, index) => index + 1); }, previousMonth() { // 实现切换到上一个月的逻辑 }, nextMonth() { // 实现切换到下一个月的逻辑 }, }, }; </script> <style> .calendar { /* 样式 */ } .header { /* 样式 */ } .days { /* 样式 */ } .day { /* 样式 */ } .events { /* 样式 */ } .event { /* 样式 */ } </style> ``` 这个例子中的日历组件包含一个头部,显示当前月份,以及上一个月和下一个月的按钮。接下来是一个天数的区域,以及事件的区域。你可以根据自己的需求来自定义样式。 这个示例中的事件是固定的,你可以根据你的具体需求从后端获取事件数据,并在`events`数组中进行动态渲染。 在`previousMonth`和`nextMonth`方法中,你可以实现切换到上一个月和下一个月的逻辑,例如更新`currentMonth`和`days`数据,以及获取新月份的事件数据。 希望这个示例能帮到你!如果有任何问题,请随时提问。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值