vue+fullcalendar实现读取接口数据实现日历日程事件显示

本文介绍了在不理解AntDesign日历组件显示逻辑的情况下,如何使用FullCalendar作为替代方案。通过yarn或npm安装FullCalendar及相关插件,然后在Vue项目中集成并配置FullCalendar,包括显示语言、视图切换、事件处理等功能。同时,展示了如何从API获取数据并填充到日历事件中,提供了一个完整的代码实现示例,并附带了实际效果截图。

本来打算使用antdesign来实现的,但是奈何没看懂他的徽标在calendar组件里的显示逻辑(我太菜了hhhhhhhh),所以网上找了一番,发现fullcalendar是个不错的替代插件。

安装

yarn add  @fullcalendar/vue @fullcalendar/core @fullcalendar/daygrid @fullcalendar/interaction @fullcalendar/timegrid --save

npm方式安装也同理,不再赘述

代码实现

<template>
  <FullCalendar :options="calendarOptions" />
</template>

<script>
// 引入fullcalendar的部分插件
import FullCalendar from "@fullcalendar/vue";
import dayGridPlugin from "@fullcalendar/daygrid";
import timeGridPlugin from "@fullcalendar/timegrid";
import interactionPlugin from "@fullcalendar/interaction";

// 引入我在其他文件里自己定义的获取接口方法
import { getCA }  from '@/api/getCalendar';

export default {
  components: {
    FullCalendar // 引入fullcalendar模板
  },
  data () {
    return {
    // 配置fullcalendar,除了events数组,剩下的都可以不动
      calendarOptions: {
        plugins: [
          dayGridPlugin,
          timeGridPlugin,
          interactionPlugin, // needed for dateClick
        ],

        locale: 'zh-cn', // 切换语言,当前为中文
        headerToolbar: {
          left: "prev,next today",
          center: "title",
          right: "dayGridMonth,timeGridWeek,timeGridDay",
        },
        buttonText: {
          today: '今天',
          month: '月',
          week: '周',
          day: '日',

        },

        initialView: "dayGridMonth",// 指定默认显示视图
        selectable: true,
        selectMirror: true,
        dayMaxEvents: true,
        weekends: true,
        allDayDefault:false,
        // eventDisplay:'block',
        moreLinkContent :'更多',
        dayMaxEventRows :true,
        slotDuration : "00:10:00",  //一格时间槽代表多长时间,默认00:30:00(30分钟)
        slotMinTime: "08:00:00",
        slotMaxTime: "24:00:00",
        allDayText : "全天",
        handleWindowResize:true,//是否随浏览器窗口大小变化而自动变化。
        lazyFetching : true,        //是否启用懒加载技术--即只取当前条件下的视图数据,其它数据在切换时触发,默认true只取当前视图的,false是取全视图的
        firstDay: '1', // 设置一周中显示的第一天是周几,周日是0,周一是1,以此类推
        selectEventOverlap: false,
        events: [
          // {
          //   id: 111,
          //   title: '任务1',
          //   start: '2022-08-01',
          //   end: '2022-08-11',
          //   color: '#ffcc99',
          //   editable: true, //允许拖动缩放,不写默认就是false
          //   overlap: true, //允许时间重叠,即可以与其他事件并存,不写默认就是false
          //   }
          ]
      }
    }
  },
  methods: {
  // getList就是我把获取的接口数据写进calender的方法
    async getList() {
     const res  = await getCA()
     console.log(res.data.entry)
    console.log(res.data.entry.length);
     for (let i = 0; i< res.data.entry.length ;i++) {
      var obj = new Object()
      obj.id = res.data.entry[i].id,
      obj.title = res.data.entry[i].xiangmumingcheng + '——' + res.data.entry[i].lichengbeimingcheng
      obj.start = res.data.entry[i].created.slice(0,10)
      obj.end = res.data.entry[i].jiaofushijian.slice(0,10)
      obj.color = '#ffcc11'
      obj.overlap = true
      obj.editable = true
      console.log(res.data.entry[i].jiaofushijian.slice(0,10));
      this.calendarOptions.events.push(obj)
     }
    }
  },
  created () {
    this.getList()
  }
}
</script>

效果图

使用fullcalendar实现日程安排显示

希望对你有帮助!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值