简体   繁体   English

如何在 FullCalendar 中的 Event Click 上突出显示整个事件

[英]How to highlight the entire event on Event Click in FullCalendar

I am trying to highlight an event on my calendar when user clicks.当用户点击时,我试图在我的日历上突出显示一个事件。 I already have some basic code, but it's not working as i expected.我已经有了一些基本代码,但它没有按我的预期工作。 The way my code works now it's just highlighting for a week and not until the end of the event as it is shown in the calendar.我的代码现在的工作方式只是突出显示一周,而不是直到事件结束,因为它显示在日历中。 Here's some of my code这是我的一些代码

document.addEventListener('DOMContentLoaded', function() {
  var calendarEl = document.getElementById('calendar');
  var lang = document.getElementsByTagName('html')[0].getAttribute('lang');
  if(lang == 'hy'){
    var language = hyLocale;
  }else if(lang == 'ru'){
    var language = ruLocale;
  }else{
    var language = 'en';
  }
  var calendar = new Calendar(calendarEl, {
    locales: [ ruLocale, hyLocale],
    locale: language,
    plugins: [dayGridPlugin],
    events: '/events/get',
    eventClick: function(info) {

      // This is where it resets the highlight when user clicks on another event
      $('.fc-event-container a').css('background-color', '#3788d8');


      // This is where i set the background color of the event when user clicks
      $(info.el).css('background-color', '#00da4a');
      $('#info-container').empty();

      var html = 'basic html for div';
      $('#info-container').append(html);
    }

  });

And here's what it looks like on the front end.这就是它在前端的样子。

在此处输入图像描述

I want to highlight the entire event even on the next month.即使在下个月,我也想突出整个活动。

I have been doing something similar.我一直在做类似的事情。 So, I would like to share my idea for you.所以,我想和你分享我的想法。 See if it works for you.看看它是否适合你。 What I did is to assign an id to each event at my code, like, Id for Event 1 is: no1, for Event 2 is: no2 etc. Than I used eventRender to assign the id of event as a class name to the event, so it will assign that to each element where the event is stretched.我所做的是在我的代码中为每个事件分配一个 ID,例如,事件 1 的 ID 为:no1,事件 2 的 ID 为:no2 等。然后我使用eventRender将事件的 ID 作为 class 名称分配给事件,因此它将分配给事件被拉伸的每个元素。 The code would be as follow:代码如下:

eventRender: function(info) {
    info.el.className += " " + info.event.id + " ";
}

Than in eventClick, use that class to assign your css at run time, as follow:在 eventClick 中,使用该 class 在运行时分配您的 css,如下所示:

eventClick: function (info) {
    $('.'+info.event.id).css("background-color", "#00da4a");
}

Note that it assigned css but to remove that css and to apply to other event element, I will leave that logic upto you:)请注意,它分配了 css 但要删除该 css 并应用于其他事件元素,我将把这个逻辑留给你:)

UPDATE:更新:

When next or previous clicked, and event is stretching till next or previous month, you may need to use global event id variable to store selected event id.当单击下一个或上一个,并且事件延伸到下个月或上个月时,您可能需要使用全局事件 id 变量来存储选定的事件 id。 And on next and previous button click, you may use that selected event id to change color of that event.在下一个和上一个按钮单击时,您可以使用该选定的事件 ID 来更改该事件的颜色。

For example:例如:

var gEventId = 0;
document.addEventListener("DOMContentLoaded", function () {
   // First Call the Calendar Render code here...
   $('.fc-next-button, .fc-prev-button').click(function(){
      if (gEventId > 0)
      {
         $('.'+ gEventId).css("background-color", "#00da4a");
      }
   });
});

The gEventId is saved in eventClick as follow: gEventId 保存在 eventClick 中,如下所示:

eventClick: function (info) {
   gEventId = info.event.id;
   $('.'+ info.event.id).css("background-color", "#00da4a");
}

To roll back selected event id to 0, you may click that event again or provide some other mean to unselect the event.要将选定的事件 id 回滚为 0,您可以再次单击该事件或提供其他方法来取消选择该事件。 The logic is simple, you need to check if clicked event id is same as already selected event id (value stored in gEventId), than set gEventId to 0 and unselect the highlighted background else highlight the background color and set gEventId to event id.逻辑很简单,您需要检查单击的事件 id 是否与已选择的事件 id 相同(存储在 gEventId 中的值),然后将 gEventId 设置为 0 并取消选择突出显示的背景,否则突出显示背景颜色并将 gEventId 设置为事件 id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM