1

我想使用诸如引导导航导航选项卡之类的菜单来显示和隐藏事件。

在选项卡上时,我单击 Event1 仅显示带有“cid:“1””的事件,Event2 仅显示带有“cid:“2””的事件,并且 ALL 显示所有事件。

谁能帮我开发这个?基于这个问题Fullcalendar v5 how to show and hide events with checkbox我制作了一个基于复选框的代码笔,仅用于启动... https://codepen.io/berettatim/pen/rNeXwLm

html:

<div class="row">
  <div class="col-md-12">
    <ul class="nav nav-tabs">
    <li onclick="javascript:CalendarTypeSet(1)" class="nav-item"><a class="nav-link active" data-toggle="tab" href="#CalTab1" style="font-size:16px;"><b>EVENT1</b></a></li>
    <li onclick="javascript:CalendarTypeSet(2)" class="nav-item"><a class="nav-link" data-toggle="tab" href="#CalTab2" style="font-size:16px;"><b>EVENT2</b></a></li>
    <li onclick="javascript:CalendarTypeSet(3)" class="nav-item"><a class="nav-link" data-toggle="tab" href="#CalTab3" style="font-size:16px;"><b>EVENT3</b></a></li>
    <li onclick="javascript:CalendarTypeSet('all')" class="nav-item"><a class="nav-link" data-toggle="tab" href="#CalTabAll" style="font-size:16px;"><b>ALL</b></a></li>
</ul>
  </div>
  <div class="col-md-12">
    <input class="cs" value="1" type="checkbox" checked>Calendar1<br>
    <input class="cs" value="2" type="checkbox" checked>Calendar2
    <div id='calendar'></div>
  </div>

脚本:

document.addEventListener("DOMContentLoaded", function () {
  var calendarEl = document.getElementById("calendar");
  var calendar = new FullCalendar.Calendar(calendarEl, {
    now: "2020-07-11",
    scrollTime: "00:00",
    aspectRatio: 1.8,
    headerToolbar: {
      left: "today prev,next",
      center: "title",
      right: "dayGridMonth,timeGridWeek,timeGridDay,listMonth"
    },
    initialView: "dayGridMonth",
    events: function (fetchInfo, successCallback, failureCallback) {
      successCallback([
        {
          id: "1",
          title: "event1",
          start: "2020-07-01 19:30",
          end: "2020-07-02 19:30",
          backgroundColor: "#39CCCC",
          cid: "1"
        },
        {
          id: "2",
          title: "event2",
          start: "2020-07-09 19:30",
          end: "2020-07-10 19:30",
          backgroundColor: "#F012BE",
          cid: "2"
        }
      ]);
    },
    eventDidMount: function (arg) {
      var cs = document.querySelectorAll(".cs");
      cs.forEach(function (v) {
        if (v.checked) {
          if (arg.event.extendedProps.cid === v.value) {
            arg.el.style.display = "block";
          }
        } else {
          if (arg.event.extendedProps.cid === v.value) {
            arg.el.style.display = "none";
          }
        }
      });
    }
  });
  calendar.render();

  var csx = document.querySelectorAll(".cs");
  csx.forEach(function (el) {
    el.addEventListener("change", function () {
      calendar.refetchEvents();
      console.log(el);
    });
  });
});

//Not sure for this...
    function CalendarTypeSet(fTip)
    {
         var x = document.getElementById('cal_tip');
         x.value=fTip;
         //$('#calendar').fullCalendar('rerenderEvents');
    calendar.rerenderEvents('#calendar');
    }
4

1 回答 1

2

您需要的更改更多的是与选项卡的使用有关,而不是fullCalendar 3和5之间的区别。与复选框相比,一次只能选择一个选项,因此您需要从选项卡本身找出当前打开的是哪个选项卡.

所以这是一个可行的简单想法:当显示一个选项卡时,运行一个函数。在该函数中,调用日历的refetchEvents()方法 ( https://fullcalendar.io/docs/Calendar-refetchEvents )。然后,您可以使用日历的eventDataTransform回调来决定是否应在日历上显示该事件,具体取决于哪个选项卡当前处于“活动状态”。请参阅(https://fullcalendar.io/docs/eventDataTransform) - 这比它更可取,eventDidMount因为它在事件呈现到日历之前运行,而不是之后运行。

这样的事情应该这样做:

HTML:

<div class="row">
  <div class="col-md-12">
    <ul class="nav nav-tabs">
      <li data-tabid="1" class="nav-item"><a class="nav-link active" data-toggle="tab" href="#CalTab1" style="font-size:16px;"><b>EVENT1</b></a></li>
      <li data-tabid="2" class="nav-item"><a class="nav-link" data-toggle="tab" href="#CalTab2" style="font-size:16px;"><b>EVENT2</b></a></li>
      <li data-tabid="3" class="nav-item"><a class="nav-link" data-toggle="tab" href="#CalTab3" style="font-size:16px;"><b>EVENT3</b></a></li>
      <li data-tabid="all" class="nav-item"><a class="nav-link" data-toggle="tab" href="#CalTabAll" style="font-size:16px;"><b>ALL</b></a></li>
    </ul>
  </div>
</div>
<div class="row">
  <div class="col-md-12">
    <div id="calendar"></div>
  </div>
</div>

JavaScript:

document.addEventListener("DOMContentLoaded", function () {
  var calendarEl = document.getElementById("calendar");
  var calendar = new FullCalendar.Calendar(calendarEl, {
    initialDate: "2020-07-01",
    aspectRatio: 1.8,
    headerToolbar: {
      left: "today prev,next",
      center: "title",
      right: "dayGridMonth,timeGridWeek,timeGridDay,listMonth"
    },
    initialView: "dayGridMonth",
    events: function (fetchInfo, successCallback, failureCallback) {
      successCallback([
        {
          id: "1",
          title: "event1",
          start: "2020-07-01 19:30",
          end: "2020-07-02 19:30",
          backgroundColor: "#39CCCC",
          cid: "1"
        },
        {
          id: "2",
          title: "event2",
          start: "2020-07-09 19:30",
          end: "2020-07-10 19:30",
          backgroundColor: "#F012BE",
          cid: "2"
        }
      ]);
    },
    eventDataTransform: function (eventData) {
      var selectedTab = document.querySelector(".nav-tabs li a.active").dataset.tabid;
          if (selectedTab == "all" || eventData.cid == selectedTab) {
            return eventData;
          }
          else return false; //discard the event
    }
  });
  calendar.render();

  $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      calendar.refetchEvents();
  });
});

演示:https ://codepen.io/ADyson82/pen/abZZEbM

于 2020-10-19T10:08:37.717 回答